Jump to content

Very Slow Query


dweb

Recommended Posts

Hi

 

I have a 5 database tables that i'm tying together, each table has about 50,000 records.

 

The query i'm using is

 

SELECT u.id, u.name, u.age, p.name, p.code, a.time, a.kin, b.lime, g.happen, (SELECT COUNT(*) FROM `log` WHERE `logid` = u.id) AS tot
FROM users u
LEFT JOIN products p ON p.id = u.prodid
LEFT JOIN ages a ON a.id = u.ageid
LEFT JOIN book b ON b.id = u.bookid
LEFT JOIN group g ON g.id = u.groupid
WHERE u.name != 'admin'
ORDER BY u.id DESC LIMIT 10,40

 

works great but for some reason it's taking around 100 seconds to return data.

 

but if I remove

 

ORDER BY u.id DESC

 

then the query takes around 5 seconds

 

I've read in some groups that to speed up, I should do the ORDER after the query, by doing

 

SELECT * FROM (

SELECT u.id, u.name, u.age, p.name, p.code, a.time, a.kin, b.lime, g.happen, (SELECT COUNT(*) FROM `log` WHERE `logid` = u.id) AS tot
FROM users u
LEFT JOIN products p ON p.id = u.prodid
LEFT JOIN ages a ON a.id = u.ageid
LEFT JOIN book b ON b.id = u.bookid
LEFT JOIN group g ON g.id = u.groupid
WHERE u.name != 'admin'

) ORDER BY u.id DESC LIMIT 10,40

 

But that doesn't seem to work

 

Can anyone help or suggest an alternative

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/272599-very-slow-query/
Share on other sites

Two things:

 

1. Have you indexed the columns that are being used in your JOINs

 

2. Instead of doing the subquery, just JOIN the log records onto the query and GROUP by them to get the count

 

SELECT u.id, u.name, u.age,
   p.name, p.code,
   a.time, a.kin,
   b.lime,
   g.happen,
   COUNT(logid) AS tot
FROM users u
LEFT JOIN products p ON p.id = u.prodid
LEFT JOIN ages a ON a.id = u.ageid
LEFT JOIN book b ON b.id = u.bookid
LEFT JOIN group g ON g.id = u.groupid
LEFT JOIN log l ON logid = u.id AS tot
WHERE u.name != 'admin'
GROUP BY logid
ORDER BY u.id DESC
LIMIT 10,40

Link to comment
https://forums.phpfreaks.com/topic/272599-very-slow-query/#findComment-1402696
Share on other sites

Normally you would index columns used in your WHERE clause as these are the conditions it has to search.

 

You would also index columns used to JOIN your tables. When you JOIN tables the conditions for the JOIN are treated just like a WHERE condition even if not directly in a WHERE clause (i.e. using the ON or USING conditions). In fact, it is probably more important to index columns used for JOINing your tables than those that may only be used in a WHERE clause.

Link to comment
https://forums.phpfreaks.com/topic/272599-very-slow-query/#findComment-1402839
Share on other sites

Thanks, i changed the table a bit and indexed the column `name` in the `users` table seems to have made it much faster

 

I have quite a few inserts, updates and deleted from the `users` table, what do I need to do to update the index each time, or is this automatically done for me?

 

Do i have to add another query to each insert, update and delete?

 

thank you

Link to comment
https://forums.phpfreaks.com/topic/272599-very-slow-query/#findComment-1403017
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.