I have been working on speeding up a query I\'m using for about a week now and asked several questions about it here ( How can I speed up fetching the results after running an s
Consider using covering indices on the tables involved in your query.
You are indeed fetching a limited amount of columns in your select
statement and the corresponding inner join
and where
clauses. By using a covering index with the columns well ordered in it, you should get a very fast query, that is you will remove the scan table
in favor of an search table using a covering index.
Try to use those indices on your tables:
CREATE INDEX `fk_covering_feature` ON `feature` (`msrun_msrun_id`,`mzMin`,`mzMax`,`rtMin`,`rtMax`,`feature_table_id`);
CREATE INDEX `fk_covering_spectrum` ON `spectrum` (`msrun_msrun_id`,`scan_start_time`,`spectrum_id`);
CREATE INDEX `fk_covering_MSMS_precursor` ON `MSMS_precursor` (`spectrum_spectrum_id`,`ion_mz`,`precursor_id`);
When going for speed, you should also hint the query planner to understand msrun_msrun_id is a constant to check for both feature
and spectrum
tables. Add the constant test in your query by putting this additional test at the end of the query (and pass spectrumFeature_InputValues
twice):
"AND spectrum.msrun_msrun_id = ?"