I have page with simple sql queries:
Visitor is on page 940 so I need to find products to show on this page:
SELECT *
FROM items
WHERE stock = 1 AND hide = 0
ORDER BY id DESC
LIMIT 36
OFFSET 33804
this query takes 15463.242 ms
.
Also I need to show filter with manufacturers, sizes and store info for all products in stock:
SELECT MANUFACTURER, sizes, store
FROM items
WHERE stock = 1 AND hide = 0
this takes 17996.684 ms
.
I don't understand why it takes so much time.
Structure of table:
id int(11) Auto Increment
ID_PRODUCT varchar(200)
PRODUCT varchar(200)
DESCRIPTION mediumtext
URL varchar(300)
PRICE_VAT int(7)
MANUFACTURER varchar(150)
CATEGORY varchar(150)
IMGSURL varchar(3000)
catids varchar(30)
sizes varchar(30)
store varchar(30)
stock int(1)
hide int(1)
Table info:
Data size: 327 974 912
Index size: 12 075 008
Free space: 4 194 304
Rows: 310 823
It uses InnoDB and mysql 5.5.5-10.0.29-MariaDB-0+deb8u1.
Can you please help me what is wrong with this queries? Visitors can't wait over 30 seconds. Also the CPU is more than 100% while doing hte query.
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `items` ADD INDEX `items_idx_stock_hide_id` (`stock`,`hide`,`id`);
ALTER TABLE `items` ADD INDEX `items_idx_id` (`id`);
SELECT
*
FROM
items
WHERE
items.stock = 1
AND items.hide = 0
ORDER BY
items.id DESC LIMIT 36 OFFSET 33804