Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Query slows down when more IDs are added

Hello so I am facing an issue with my SQL query. I need to get the average location for each ID on the maximal day. While the query below gives me the correct result it seems that the run time grows exponential depending on the number of IDS. So while it is almost real time for 1 or 2 IDS it takes a few minutes for 3 and very long for 5. So running 5 queries for 5 IDs is a lot faster than running a single one containing all 5.
(I have a lot of IDs for which I need to check this, also the database is quite big).

How does this happen and how do I avoid it?

My guess would be that the problem comes from having the same ID list selected twice but I am not sure how to fix it/ if this really is the problem. I toke a look at all the similar issues on stack overflow but I did not find any which was answering my problem.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(I am using a Mircosoft SQL Server).

SELECT t.ID, MAX(date) as MAX(date), AVG(Latitude) as lat,  AVG(Longitude) as long
From table1  t
inner Join (
    select ID, max(date) as maxdate
    from table1 t2
    WHERE ID IN ('1', '2', '3')
    group by ID
    )
tm on t.ID = tm.ID and t.date = tm.maxdate
WHERE t.ID IN ('1', '2', '3')
GROUP BY t.ID

>Solution :

You can give window functions a try:

WITH cte AS (
    SELECT *, RANK() OVER (PARTITION BY id ORDER BY date DESC) AS rn
    FROM t
    WHERE id IN ('1', '2', '3')
)
SELECT *
FROM cte
WHERE rn = 1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading