4
SELECT mt.Test_ID,
       mtp.Test_Plan_Doc_No,
       mp.Group_Name,
       mp.Point_Name,
       mp.Limit_1,
       mp.Limit_2,
       mp.Datatype,
       mt.Start_Date,
       mv.Measurement_Value,
       mv.Status_Value
FROM   measurement_test mt
       INNER JOIN measurement_values mv
               ON mt.Test_ID = mv.Test_ID
       INNER JOIN measurement_point mp
               ON mv.Point_ID = mp.Point_ID
       INNER JOIN measurement_test_plan mtp
               ON mtp.Test_ID = mt.Test_ID
WHERE  mtp.Test_Plan_Doc_No IN ( 'test1', 'test2' )
       AND mp.Group_Name = 'gp_name'

hello guys.

The above is the query that i am executing to create a view.

But i have a problem as follows:

the measurement value table has around 82 million rows. the measurement point table has around say 5 million rows measurement test plan and measurement test tables have around 9000 - 100000 rows.

My problem is when i execute the above query the execution time is 8 minutes and the result that is generated has only 400 rows.

Is there any possible way to reduce the execution time of the above query?

NOTE : I am using the above query in my web page

9
  • what are the rough duration of execution for the said query?
    – ianace
    Aug 28, 2012 at 8:54
  • 2
    have you created necessary indices
    – diEcho
    Aug 28, 2012 at 8:54
  • @ianace : its around 8 minutes
    – sankar
    Aug 28, 2012 at 8:55
  • If you run explain on the query does it make use of your indexes? If not, can you now see some to add that will help?
    – BugFinder
    Aug 28, 2012 at 8:55
  • Please post output of SHOW CREATE TABLE xxx with xxx in (measurement_test, measurement_values, measurement_point, measurement_test_plan) Aug 28, 2012 at 8:56

3 Answers 3

4

First off, for an efficient join, you should keep the tables in increasing order of number of rows. This reduces the number of row scans drastically. So for your query, the join order should be measurement_test mt natural join measurement_point mp natural join measurement_values mv. Also ensure that the join columns have indexes defined on them, and they have exactly identical datatypes. char(15) and varchar(15) are considered similar in MySQL, but not char(15) and varchar(10).

1
  • On 2nd thoughts, after going through a lot of other Stackoverflow posts and related articles on internet, I think that the order of the join columns is immaterial. Whatever be the way, it's always going to be a Cartesian product. The indexes and column types have to be taken care of, of course.
    – SexyBeast
    Sep 13, 2012 at 19:14
2

reference from you @comment you said u did not create any index on any table. So CREATE INDEX on those column on which are used in WHERE and ON condition

in your case create index on

mt.Test_ID, 
mv.Test_ID, 
mv.Point_ID,
mp.Point_ID, 
mtp.Test_ID ,
mp.group_name ,
mtp.Test_Plan_Doc_No

Here I Rewrite your Query:

SELECT mt.Test_ID,
       mtp.Test_Plan_Doc_No,
       mp.Group_Name,
       mp.Point_Name,
       mp.Limit_1,
       mp.Limit_2,
       mp.Datatype,
       mt.Start_Date,
       mv.Measurement_Value,
       mv.Status_Value
FROM   measurement_test mt
       INNER JOIN measurement_values mv USING(Test_ID)            
       INNER JOIN measurement_point mp USING (Point_ID)
       INNER JOIN measurement_test_plan mtp USING(Test_ID)
WHERE  mtp.Test_Plan_Doc_No IN ( 'test1', 'test2' )
       AND mp.Group_Name = 'gp_name'
0

Make sure you have proper indexes for each column used in the WHERE clause.

You can use a query analyzer to see whether or not the indexes are used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.