简体   繁体   English

我的 sql 查询在 7 亿行表中花费时间也有限制

[英]My sql query taking time in 700 million rows table with limit also

Hello team,你好团队,

I am facing issue related to fetch data from a table of 700 million rows with limit also.我面临着与从 7 亿行的表中获取数据相关的问题,并且也有限制。 Below is the complete detail of our scenario:-以下是我们场景的完整细节:-

I am having a table with 700 million rows here is table structure:-我有一个有 7 亿行的表,这里是表结构:-

   CREATE TABLE `product_items` (
      `id` int(11) NOT NULL,
      `product_id` int(11) NOT NULL,
      `item_id` int(11) NOT NULL,
      `model_id` int(11) DEFAULT NULL,
      `value` varchar(255) NOT NULL,
      `engine_size` varchar(255) DEFAULT NULL,
      `position` varchar(255) DEFAULT NULL,
      `vehicle_attributes` varchar(255) DEFAULT NULL,
      `application_notes` varchar(255) DEFAULT NULL,
      `status` tinyint(1) NOT NULL DEFAULT '1',
      `created_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `product_items` ADD INDEX(`product_id`); 
ALTER TABLE `product_items` ADD INDEX(`item_id`); 
ALTER TABLE `product_items` ADD INDEX(`value`); 
ALTER TABLE `product_items` ADD INDEX(`model_id`); 
ALTER TABLE `product_items` ADD INDEX(`engine_size`); 


ALTER TABLE `product_items` ADD PRIMARY KEY (`id`);

在此处输入图像描述

Related to this data Product_id =2 model_id = 1615 item_id =1 total 2 results are there.与此数据相关 Product_id =2 model_id = 1615 item_id =1 total 2 results are there.

when i run this query It give results in.5 secs as per screenshot:当我运行此查询时,它会根据屏幕截图在 5 秒内给出结果:

SELECT `product_items`.* FROM (`product_items`)  WHERE `product_items`.`status` =  '1' AND `product_items`.`item_id` =  1
AND `product_items`.`product_id` =  '2' AND `product_items`.`model_id` =  '1615'  limit 2;

![在此处输入图片描述

But when i run this query:-但是当我运行这个查询时:-

 SELECT `product_items`.* FROM (`product_items`)  WHERE `product_items`.`status` =  '1' AND `product_items`.`item_id` =  1
    AND `product_items`.`product_id` =  '2' AND `product_items`.`model_id` =  '1615'  limit 10;

在此处输入图像描述

It take takes too much time and do not show results after 20 minutes.这需要太多时间,并且在 20 分钟后不显示结果。

We are using digital-ocean database service with 6 CPU + 16 GB + 270 GB Disk我们正在使用 6 CPU + 16 GB + 270 GB 磁盘的数字海洋数据库服务

If anyone can help us in this case will be much appreciated.如果有人能在这种情况下帮助我们,我们将不胜感激。

You have to add some indexes, one of them is this one:您必须添加一些索引,其中之一是:

ALTER TABLE `product_items` ADD INDEX `product_items_index` (status, item_id, product_id, model_id)

Also add primary key constraint:同时添加主键约束:

ALTER TABLE product_items ADD PRIMARY KEY(id);

for more info check here 欲了解更多信息,请在此处查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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