0

Before i start my question i cover briefly what the problem is:

I have a table that stores around 4 million 'parameter' values. These values have an id, simulation id and parameter id.

The parameter id maps to a parameter table that basically just maps the id to a text like representation of the parameter x,y, etc etc

The simulation table has around 170k entries that map parameter values to a job.

There is also a score table which stores the score of each simulation , simulations have varying scores for example one might have one score another might have three. The scores tables has a simulation_id column for selecting this.

Each job has an id and an objective.

Currently im trying to select all the parameter_values who's parameter is 'x' and where the job id is 17 and fetch the score of it. The variables of the select will change but in princible its only really these things im interested in.

Currently im using this statement:

SELECT simulation.id , value , name , ( SELECT GROUP_CONCAT(score) FROM score WHERE score.simulation_id = simulation.id ) AS score FROM simulation,parameter_value,parameter WHERE simulation.id=parameter_value.simulation_id AND simulation.job_id = 17 AND parameter_value.parameter_id=parameter.id AND parameter.name  = "$x1"

This works nicley except its taking around 3 seconds to execute. Can this be done any faster?

I don't know if it would be faster doing a query before this a pre-calculating the parameter_ids im searching for and doing an WHERE parameter_id IN (1,2,3,4) etc.

But i was under the impression SQL would optimize this anyway?

I have created index's where ever possible but cant get faster than the 2.7 seconds mark.

So my question would be: Should i pre-calculate some values and avoid the joins, Is there another other than group_concat to get the scores and is there any other optimizations i could make to this?

I should also add that the scores must be in the same row or at least return sorted so i can easily read them from the result set.

Thanks, Lewis

2
  • The query optimizer will try to take the optimum path toward satisfying your query, but it isn't magic. First step is to run EXPLAIN SELECT (the rest of your query) from the command line client and post the result... without looking at the plan the optimizer is choosing and trying to understand why, it's all speculation and guesswork. Nov 16, 2013 at 1:21
  • Brilliant , ive had a look at it using Explain and added index's for the relevant keys. It seems i had missed one. I also removed the third join and decided to use the parameter id's directly. This has brought it down to around 1.4 seconds. Dont think ill be able to get it much lower.
    – Lewis GC M
    Nov 16, 2013 at 2:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.