Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help Improve Query Performance  RSS feed

 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a query which counts the amount of work that some workers have done in a given day. In its most simple form the query is



The actual query has even more joins than I have shown here but this is the core of it.

The problem is that this query is really slow. Can anyone offer suggestions to speed it up?

One idea is to create a seperate table with only 2 columns (workCount,workDate), and to have a trigger update this table. Is this feasible? Is there a better way?

Thanks.
 
author & internet detective
Marshal
Posts: 38250
623
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jehan,
Since there are no where conditions, there it it's harder to tune the query. If you are joining on something that isn't a key, there might still be scope to add an index.

Your idea to create a calculation summary table is a good one though. Presuming that updates are a lot less frequent than reads, that sounds like the way to go.
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Thanks for taking the time to help me, I appreciate it.

So if I were to create a calculation summary table, I would probably need a trigger to update it from the source table right?

Can you please offer some suggestions on how I can write this trigger based on the query I provided? Because I have the count, group by and joins the trigger syntax is not clear for me based on the simple examples I found online. I am using a MySQL DB.

Thanks.
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to advise how to optimize a query we haven't seen. Besides ensuring that all joins happen on indexed columns (as Jeanne already suggested), you may want to take a look at the query plan MySQL executes to make sure there are few (or no) table scans involved: http://dev.mysql.com/doc/refman/5.1/en/execution-plan-information.html
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!