jaymc Posted March 1, 2007 Share Posted March 1, 2007 I have a cron job execute a script every 1 minute That script queries a table and drags out 15,000 rows, and for each row then executes another query on updating a table So thats basically 15,000 queries.. Its killing the server when it runs and locking mysql quite heavily Rather than have 15,000 seperate queries I was think of using 1 query and structuring it as so WHERE IN ID (1,2,3,4,5,6,7.......15,000) A whopping single query, but could it cope with 15,000 'IN IDs' and would it be better than just having 15,000 single queries Quote Link to comment https://forums.phpfreaks.com/topic/40713-optimize-query/ Share on other sites More sharing options...
fenway Posted March 1, 2007 Share Posted March 1, 2007 15K queries is always going to be slower... yes, an IN clause with 15K uids won't be ideal (though index merge in 5.0 might handle it better), but that's much preferred. Quote Link to comment https://forums.phpfreaks.com/topic/40713-optimize-query/#findComment-197046 Share on other sites More sharing options...
shoz Posted March 1, 2007 Share Posted March 1, 2007 You may be able to use a single query to do the UPDATE (ie not having to do the first SELECT), but you'd have to explain what the UPDATE is doing in more detail. A code snippet should suffice. Quote Link to comment https://forums.phpfreaks.com/topic/40713-optimize-query/#findComment-197076 Share on other sites More sharing options...
jaymc Posted March 1, 2007 Author Share Posted March 1, 2007 What if I use the in ID method with 700 entreies rather than 15,000 Would that be fine? Ive actually thought of a way to reduce the rows your see So basically WHERE IN ID(1,2,3,4,5,6,7,8....700) Quote Link to comment https://forums.phpfreaks.com/topic/40713-optimize-query/#findComment-197448 Share on other sites More sharing options...
jaymc Posted March 2, 2007 Author Share Posted March 2, 2007 Edit: Whats better 700 queries or an IN ID with 700 ID's Quote Link to comment https://forums.phpfreaks.com/topic/40713-optimize-query/#findComment-197506 Share on other sites More sharing options...
Vikas Jayna Posted March 2, 2007 Share Posted March 2, 2007 How about using a subquery:- WHERE ID IN (SELECT ID FROM TABLENAME WHERE blah blah ...) A single query like this is certainly going to be better (provided the column ID is indexed) Quote Link to comment https://forums.phpfreaks.com/topic/40713-optimize-query/#findComment-197646 Share on other sites More sharing options...
fenway Posted March 2, 2007 Share Posted March 2, 2007 That's still using an IN clause, so it's about the same. Quote Link to comment https://forums.phpfreaks.com/topic/40713-optimize-query/#findComment-198068 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.