简体   繁体   中英

MySql Query Slow On Very Large Table

I have a table with 85,000 records, which will grow daily!

I am running a query for example:

SELECT * FROM `table`  
WHERE `start_timestamp` >= 'TIMESTAMP'  
AND `end_timestamp` <= 'TIMESTAMP'

This query is literally taking forever to run on 85,000 rows, never mind 1,000,000

Now the field for the timestamp is actually a large integer holding a timestamp from php. The query is run through php with codeigniter query()

It runs pretty quick if the date range < 31 days, but anything above that it's extremely slow. I also have indexes on those 2 columns on the where clause.

These are some suggestions to improve the performance of the query

  1. Replace SELECT * by the only fields you want
  2. Add indexing on the table fields you want as output
  3. Apply LIMIT tag as and when required. Don't select all the records
  4. Fire two different queries: one for counting total number of records and other for fetching number of records per page (eg 10, 20, 50, etc...)
  5. If applicable, create Database Views and get data from them instead of tables

Since start_timestamp and end_timestamp are the only criteria for your query, I suggest you create an index for them.

CREATE INDEX `idx_timestamp` ON `table` (`start_timestamp`, `end_timestamp`);

This should optimize MySQL's ability to locate the records with the specified criteria.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM