mysql - 如何在大型数据库中优化数据库这个查询?

标签 mysql database query-optimization

查询

SELECT id FROM `user_tmp` 
WHERE  `code` = '9s5xs1sy' 
  AND  `go` NOT REGEXP 'http://www.xxxx.example.com/aflam/|http://xx.example.com|http://www.xxxxx..example.com/aflam/|http://www.xxxxxx.example.com/v/|http://www.xxxxxx.example.com/vb/'  
  AND check='done'  
  AND  `dataip` <1319992460
ORDER BY id DESC 
LIMIT 50

MySQL 返回:

Showing rows 0 - 29 ( 50 total, Query took 21.3102 sec) [id: 2622270 - 2602288]

查询耗时 21.3102 秒

如果我删除

AND dataip <1319992460

MySQL 返回

Showing rows 0 - 29 ( 50 total, Query took 0.0859 sec) [id: 3637556 - 3627005]

查询耗时 0.0859 秒

如果没有数据,MySQL返回

MySQL returned an empty result set (i.e. zero rows). ( Query took 21.7332 sec )

查询耗时 21.7332 秒

解释计划:

  SQL query: Explain SELECT * FROM `user_tmp` WHERE `code` = '93mhco3s5y' AND `too` NOT REGEXP 'http://www.10neen.com/aflam/|http://3ltool.com|http://www.10neen.com/aflam/|http://www.10neen.com/v/|http://www.m1-w3d.com/vb/' and checkopen='2010' and `dataip` <1319992460 ORDER BY id DESC LIMIT 50;
    Rows: 1
    id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
    1   SIMPLE  user_tmp    index   NULL    PRIMARY     4   NULL    50  Using where

使用的数据库示例

CREATE TABLE IF NOT EXISTS user_tmp ( id int(9) NOT NULL AUTO_INCREMENT, ip text NOT NULL, dataip bigint(20) NOT NULL, ref text NOT NULL, click int(20) NOT NULL, code text NOT NULL, too text NOT NULL, name text NOT NULL, checkopen text NOT NULL, contry text NOT NULL, vOperation text NOT NULL, vBrowser text NOT NULL, iconOperation text NOT NULL,
iconBrowser text NOT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4653425 ;

--

-- 转储表 user_tmp

的数据
INSERT INTO `user_tmp` (`id`, `ip`, `dataip`, `ref`, `click`, `code`, `too`, `name`, `checkopen`, `contry`, `vOperation`, `vBrowser`, `iconOperation`, `iconBrowser`) VALUES
(1, '54.125.78.84', 1319506641, 'http://xxxx.example.com/vb/showthread.php%D8%AA%D8%AD%D9%85%D9%8A%D9%84-%D8%A7%D8%BA%D9%86%D9%8A%D8%A9-%D8%A7%D9%84%D8%A8%D9%88%D9%85-giovanni-marradi-lovers-rendezvous-3cd-1999-a-155712.html', 0, '4mxxxxx5', 'http://www.xxx.example.com/aflam/', 'xxxxe', '2010', 'US', 'Linux', 'Chrome 12.0.742 ', 'linux.png', 'chrome.png');

我想要正确的方式来查询和优化数据库

最佳答案

除了主键,你没有任何索引。您需要在 WHERE 语句中使用的字段上建立索引。如果您只需要索引 1 个字段或多个字段的组合取决于您将针对该表运行的其他 SELECT。

请记住,REGEXP 根本不能使用索引,LIKE 只有在不以通配符开头时才能使用索引(所以 LIKE 'a%' 可以使用索引,但是 LIKE '%a' 不能),大于/小于 (<>) 通常也不使用索引。

所以您只剩下codecheck 字段。我想很多行将具有相同的 check 值,因此我将以 code 字段开始索引。多字段索引只能按照定义的顺序使用...

想象一下为字段code, check 创建的索引。该索引可用于您的查询(WHERE 子句包含两个字段),也可用于只有 code 字段的查询,但不能用于只有 check 字段的查询。

ORDER BY id 重要吗?如果不是,请将其保留,这将阻止排序通过,您的查询将更快完成。

关于mysql - 如何在大型数据库中优化数据库这个查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7999134/

相关文章:

mysql - Magento - 将 magento 从 1.6.2 更新到 1.7.0.2

mysql - 需要路由中的文件才能运行查询

database - 当在 Node.js 中从 firebird 中选择时,未知值 <Buffer d2 f3 f0 e0 e5 e2 e0 20>

android - 加密数据库(sqlcipher,cacheword)的备份和恢复?

mysql - 通过更改评估顺序减少 SQL 查询的执行时间

php - 通过标签或文本字段和 MySQL 的全文搜索方法

database - 规范化两列表

mysql - 哪种数据库方案在性能方面更好?

mysql - 优化我的 MySQL 查询

php - 了解插入忽略 (MySQL) 是否实际插入到 PHP 中