简体   繁体   English

MYSQL 查询慢 SELECT DISTINCT

[英]MYSQL query slow SELECT DISTINCT

I want to output the most used search items of my classifieds of the last 3 days我要 output 最近 3 天我的分类广告中最常用的搜索项

I have an extra table where I save all search items together with the day and a counter which increments if the search is used again (and for future use the category page it was used on)我有一个额外的表,我将所有搜索项与日期一起保存,如果再次使用搜索,计数器会增加(以及将来使用它所使用的类别页面)

This works but the speed is less than desireable.这可行,但速度低于预期。

Any Ideas on how to improve the performance?关于如何提高性能的任何想法?

UPDATE: It seems that copying to temp table is causing the problem- How can i change this?更新:似乎复制到临时表导致了问题-我该如何更改?

SQL looks like: SQL 看起来像:

SELECT distinct search_item 
FROM `tb_search` 
WHERE added >= ( CURDATE() - INTERVAL 3 DAY ) 
ORDER BY count DESC 
LIMIT 0,20

EXPLAIN:解释:

id select_type table        type    possible_keys  key     key_len ref rows Extra
1   SIMPLE     tb_search    range   added          added    3       NULL 4537   Using index condition; Using temporary; Using filesort...   

But it is quite slow, it takes 12 ms of the complete 40ms execution time of the php script so 25%但它很慢,php 脚本的完整 40ms 执行时间需要 12ms 所以 25%

The table structure (Innodb) 23.000 records表结构 (Innodb) 23.000 条记录

tb_search
Column  Type    Null    Default Links to    Comments    Media (MIME) type
id (Primary)    bigint(20)  No              
search_item varchar(30) No              
category    varchar(30) Yes NULL            
added   date    No              
count   smallint(6) No              


 Indexes
    Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
    PRIMARY BTREE   Yes No  id  23744   A   No  
    search_item BTREE   No  No  search_item 11872   A   No  
    added   BTREE   No  No  added   44  A   No  

space usage空间使用

Data 1.5 MiB Index 1.9 MiB Overhead 466.0 MiB <-- This is the same for all tables???数据 1.5 MiB 索引 1.9 MiB 开销 466.0 MiB <-- 这对所有表都相同??? optimizing all tables did not help?优化所有表格没有帮助? Effective -485,048,320 B Total 3.4 MiB有效 -485,048,320 B 总计 3.4 MiB

Is the overhead a problem?开销有问题吗? How can I get rid of it?我怎样才能摆脱它? Optimizing all tables did not change it.优化所有表并没有改变它。 Total size of db is 86MB数据库的总大小为 86MB

**Detailed profile**

1   Starting    48 µs
2   Checking Permissions    6 µs
3   Opening Tables  14 µs
4   After Opening Tables    13 µs
5   System Lock 5 µs
6   Table Lock  3 µs
7   After Table Lock    5 µs
8   Init    20 µs
9   Optimizing  11 µs
10  Statistics  85 µs
11  Preparing   34 µs
12  Executing   4 µs
13  Creating Tmp Table  24 µs
**14    Copying To Tmp Table    9.7 ms**
15  Sorting Result  218 µs
16  Sending Data    18 µs
17  End 4 µs
18  Removing Tmp Table  9 µs
19  End 5 µs
20  Query End   7 µs
21  Closing Tables  9 µs
22  Freeing Items   8 µs
23  Updating Status 21 µs
24  Cleaning Up 4 µs
Summary by stateDocumentation
State
Total Time
% Time
Calls
ø Time
Starting    48 µs   0.47%   1   48 µs
Checking Permissions    6 µs    0.06%   1   6 µs
Opening Tables  14 µs   0.14%   1   14 µs
After Opening Tables    13 µs   0.13%   1   13 µs
System Lock 5 µs    0.05%   1   5 µs
Table Lock  3 µs    0.03%   1   3 µs
After Table Lock    5 µs    0.05%   1   5 µs
Init    20 µs   0.19%   1   20 µs
Optimizing  11 µs   0.11%   1   11 µs
Statistics  85 µs   0.83%   1   85 µs
Preparing   34 µs   0.33%   1   34 µs
Executing   4 µs    0.04%   1   4 µs
Creating Tmp Table  24 µs   0.23%   1   24 µs
**Copying To Tmp Table  9.7 ms  94.42%  1   9.7 ms**
Sorting Result  218 µs  2.12%   1   218 µs
Sending Data    18 µs   0.17%   1   18 µs
End 4 µs    0.04%   2   2 µs
Removing Tmp Table  9 µs    0.09%   1   9 µs
Query End   7 µs    0.07%   1   7 µs
Closing Tables  9 µs    0.09%   1   9 µs
Freeing Items   8 µs    0.08%   1   8 µs
Updating Status 21 µs   0.20%   1   21 µs
Cleaning Up 4 µs    0.04%   1   4 µs

table structure表结构

-- phpMyAdmin SQL Dump
-- version 5.0.3
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Apr 10, 2021 at 11:03 AM
-- Server version: 5.5.68-MariaDB
-- PHP Version: 7.3.27

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `terraristik_main`
--

-- --------------------------------------------------------

--
-- Table structure for table `tb_search`
--

CREATE TABLE `tb_search` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `search_item` varchar(30) NOT NULL,
  `category` varchar(30) DEFAULT NULL,
  `added` date NOT NULL,
  `count` smallint(6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tb_search`
--
ALTER TABLE `tb_search`
  ADD PRIMARY KEY (`id`),
  ADD KEY `search_item` (`search_item`),
  ADD KEY `added` (`added`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tb_search`
--
ALTER TABLE `tb_search`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

SOLUTION解决方案

By adding an index for count I reduced the time by more than 95%..!通过添加count索引,我将时间减少了 95% 以上..! Now the whole operation takes abou 1-1.5 ms.现在整个操作大约需要 1-1.5 毫秒。

The copy_to_temp table is down from 10 ms to 0.5 ms - still using 65% of all time but sufficently fast for my needs. copy_to_temp table从 10 毫秒下降到 0.5 毫秒 - 仍然使用 65% 的时间,但速度足以满足我的需要。

轮廓

Use for a date three days ago:用于三天前的日期:

SELECT distinct search_item 
FROM `tb_search t` 
WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL -3 DAY);

Or you can use:或者您可以使用:

SELECT distinct search_item 
FROM `tb_search t` 
WHERE t.date >= ( CURDATE() - INTERVAL 3 DAY );

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

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