MySQL optimization question — how to speed up a simple query
-
16-10-2019 - |
Question
I have a very simple database, one table with five columns (one INTEGER key, two VARCHAR, a DATE, and a DOUBLE). The table contains 909,000 records.
A simple query like
SELECT t.price
FROM commoddb.tbcommodprices t
WHERE
t.ticker='LAV05 Comdty'
&& t.whichprice='last'
&& t.date = '2005-09-01'
takes over 1.6 seconds to respond to a local query, which seems very slow. I would appreciate any suggestions for ways to speed this up.
Solution
Here is your original query
SELECT t.price
FROM commoddb.tbcommodprices t
WHERE t.ticker='LAV05 Comdty'
AND t.whichprice='last'
t.date = '2005-09-01';
Since there is but one table in this query, all you need is a good index.
ALTER TABLE commoddb.tbcommodprices ADD INDEX (ticker,whichprice,date);
This index should now be a permanent part of the table until you decide to remove it, which I don't think you want to do. To see the table and the indexes, just do this:
SHOW CREATE TABLE commoddb.tbcommodprices\G
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange