This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help Improve Query Performance

 
Jehan Jaleel
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.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 35976
422
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.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • 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
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!