sql >> Database >  >> RDS >> Sqlserver

TOP vertraagt ​​zoekopdracht

SELECT  TOP 1000
        *
FROM    (
        SELECT  patientId, labId, result,
                DENSE_RANK() OVER (PARTITION BY patientId, labId ORDER BY labDate DESC) dr
        FROM    labs
        ) q
PIVOT   (
        MIN(result)
        FOR
        labId IN ([1],[2],[3],[4],[5],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17])
        ) p
WHERE   dr = 1
ORDER BY
        patientId

U kunt ook proberen een geïndexeerde weergave als volgt te maken:

CREATE VIEW
        v_labs_patient_lab
WITH SCHEMABINDING
AS
SELECT  patientId, labId, COUNT_BIG(*) AS cnt
FROM    dbo.labs
GROUP BY
        patientId, labId

CREATE UNIQUE CLUSTERED INDEX
        ux_labs_patient_lab
ON      v_labs_patient_lab (patientId, labId)
SELECT TOP 1000 * FROM ( SELECT lr.patientId, lr.labId, lr.result FROM v_labs_patient_lab vl CROSS APPLY ( SELECT TOP 1 WITH TIES result FROM labs l WHERE l.patientId = vl.patientId AND l.labId = vl.labId ORDER BY l.labDate DESC ) lr ) q PIVOT ( MIN(result) FOR labId IN ([1],[2],[3],[4],[5],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17]) ) p ORDER BY patientId

  1. Hoe productbeoordelingsscores te middelen met activerecord en POSTGRES

  2. Echo JS onclick vulfunctie AJAX werkt niet

  3. Hoe Persistent Connection van PDO te gebruiken?

  4. Hoe BIT_LENGTH() werkt in MariaDB