Currently I am having an issue with slow queries to my DB – query time varies from 0.0005 seconds to 70 seconds.
Currently my table structure with content is following:
CREATE TABLE IF NOT EXISTS `content` ( `content_id` int(11) NOT NULL AUTO_INCREMENT, `content_url` text NOT NULL, `content_text` text NOT NULL, `seed_id` int(11) NOT NULL, `created_at` bigint(20) NOT NULL, `image` varchar(2000) DEFAULT NULL, `price` varchar(300) DEFAULT NULL, PRIMARY KEY (`content_id`), UNIQUE KEY `CONTENT_TEXT_UNIQUE` (`content_text`(255)), KEY `FK_SEED_CODE` (`seed_id`), KEY `CONTENT_TEXT_TIME_INDEX` (`content_text`(255),`created_at`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=111357870 ; ALTER TABLE `content` ADD CONSTRAINT `FK_SEED_ID` FOREIGN KEY (`seed_id`) REFERENCES `seed` (`seed_id`) ON DELETE CASCADE ON UPDATE CASCADE;
Currently I have only 2 queries to Database:
SELECT seed.seed_code,content.content_id as id, content.content_url, content.content_text, content.created_at, content.image, content.price FROM content
LEFT JOIN seed ON content.seed_id = seed.seed_id
WHERE seed.seed_switch = 1 AND seed.seed_status_id = 3 AND seed.seed_id in (
SELECT seed_id FROM seed WHERE storage_id ='.$storage.') '.$filter.' ORDER BY content.content_id DESC, content.created_at DESC LIMIT 50
And
SELECT seed.seed_code,content.content_id as id, content.content_url, content.content_text, content.created_at, content.image, content.price FROM content
LEFT JOIN seed ON content.seed_id = seed.seed_id
WHERE seed.seed_switch = 1 AND seed.seed_status_id = 3 AND seed.seed_id in (
SELECT seed_id FROM seed WHERE storage_id ='.$storage.') ORDER BY content.content_id DESC, content.created_at DESC LIMIT 50
Table seed contains ± 20 entries. Which doesn’t change mostly.
Indexes created on content table seems not working, because still I am having very big load time.
What could be the improvements of DB?
UPDATE 1
The content tables contains around 1mil entries and it grows every day with 1-2k entries.
$filter variable contains additional filters. So some other AND statements, which are generated randomly depending of user input. But it filters only content.text and created_at date.
EDIT
Ok, noticed the autoincrement in your create table. You have or have had millions of records (since increment is over 100 million) and are running a where-in subselect, not going to get ideal performance taking that approach. Try below query and see if that improves load times.
You haven’t supplied all the details (for example, how many records the tables in question have and what the output of
'.$filter.'is), but more than likely the subselect is the cause of the slow load time. Also, save yourself some typing and alias the tables! Cleaned up example: