简体   繁体   English

MySQL:选择查询性能问题

[英]MySQL : Select query performance issue

We are facing performance related problem with select query. 我们在选择查询时面临与性能相关的问题。 We have reports table which contains approximate 2 Crore (20 million) records. 我们有报告表,其中包含大约2千万 (2000万)的记录。

When we are executing simple count(*) from to check count, its taking more than 1 minute to display result. 当我们执行简单计数(*)从检查计数时,它花费超过1分钟来显示结果。

Here is the info about mysql, server and query 这是关于mysql,服务器和查询的信息

System info 系统信息

  OS             : Debian 6.0.7
  Model          : AMD Opteron(tm) Processor 6172
  cpu MHz        : 2100.154
  cache size     : 512 KB
  processor      : 2

Memory       total       used       free     shared    buffers     cached
Mem:         16083       6335       9747          0        153       5323

Mysql info Mysql信息

mysql  Ver 14.14 Distrib 5.1.66, for debian-linux-gnu (x86_64) using readline 6.1

my.conf settings my.conf设置

 key_buffer              = 16M
  max_allowed_packet      = 16M
  thread_stack            = 192K
  thread_cache_size       = 8
  max_connections        = 1000
  table_cache            = 128
  innodb_buffer_pool_size = 3G
  query_cache_limit       = 512M
  query_cache_size        = 3G

mysql> select count(*) from reports;
+-----------+
|  count(*) |
+-----------+
|  23311587 |
+-----------+
1 row in set (67.07 sec)

DB engine : Innodb . DB engine : Innodb

EDIT : Query execution with index and without index 编辑:使用索引执行查询而不使用索引

mysql> select count(id) from Reports USE INDEX(PRIMARY);

+-----------+
| count(id) |
+-----------+
|  17835433 |
+-----------+
1 row in set (55.56 sec)

mysql> 
mysql> select count(id) from Reports;

+-----------+
| count(id) |
+-----------+
|  17835433 |
+-----------+
1 row in set (55.65 sec)

I am struggling with performance issue, can anyone please help me to improve performance of table? 我正在努力解决性能问题,有谁能帮助我提高表的性能?

Have a look at This 看看这个

Try like 试试吧

SELECT COUNT(coupon_id) FROM coupon USE INDEX (PRIMARY);

Where coupon_id is Primary Key in coupon table coupon_id是优惠券表中的主键

This is because you are using InnoDB. 这是因为您使用的是InnoDB。

InnoDB tables are slow on a simple count(*) query, because it needs to do a full table scan for that. InnoDB表在简单的count(*)查询上很慢,因为它需要对其执行全表扫描。

You could potentially increase performance of this query, by making it use the PRIMARY index. 您可以通过使用PRIMARY索引来提高此查询的性能。

select count(reportId) from reports USE INDEX(PRIMARY);

The real question here is: do you need to perform this kind of query a lot? 这里真正的问题是:你需要经常执行这种查询吗? Mostly you would do counts with a WHERE clause in it, which, given the correct indexes, should run just fine 大多数情况下,你会使用WHERE子句进行计数,在给定正确的索引的情况下,它应该运行得很好

you can use index on the required column. 您可以在所需列上使用索引。

http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html

MySQL executes the count(*) by actually looking at the data. MySQL通过实际查看数据来执行count(*) If you have a primary key index, it is going to scan the primary key index to get the results, rather than the original data. 如果您有主键索引,它将扫描主键索引以获取结果,而不是原始数据。 I am impressed that your system performance is so consistent that the same operation took 55.56 seconds in one case and 55.65 seconds in the other -- less than half a percent difference. 令我印象深刻的是,你的系统性能如此一致,以至于同一操作在一种情况下需要55.56秒而在另一种情况下需要55.65秒 - 差异不到半个百分点。

Doing the scan requires loading the index into memory. 执行扫描需要将索引加载到内存中。 If the index doesn't fit into memory, then it will take longer. 如果索引不适合内存,则需要更长时间。 Check your system memory configuration to be sure that you can fit the 17 million records in at one time. 检查系统内存配置,确保一次可以容纳1700万条记录。 Alas, I'm not intimately familiar with all the parameters for configuring MySQL, but on a machine with 16 Gbytes of memory and a buffer pool of 3Gbytes, there should be enough memory. 唉,我并不是非常熟悉配置MySQL的所有参数,但是在具有16 GB内存和3Gbytes缓冲池的机器上,应该有足够的内存。

You may need to consider other options, if performance on this type of query is important. 如果此类查询的性能很重要,您可能需要考虑其他选项。 Ad hoc queries on a table with 17 million rows is going to take time. 对具有1700万行的表进行即席查询需要时间。 If you need speed performance, consider a data mart approach, where you extract and summarize data in ways that solve most users' problems. 如果您需要速度性能,请考虑采用数据集市方法,以解决大多数用户问题的方式提取和汇总数据。

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

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