用于JOIN大表的MySQL查询优化

时间:2015-09-02 11:34:47

标签: mysql sql innodb

我的MySQL查询存在大数据访问问题,当使用join优化查询时,它会在122秒内为一周的数据提供输出。然后对于一个月的数据,该过程需要526秒。 我想优化这个查询,每年花费更少的处理时间,或者是否有任何方法可以优化MySQL设置?

表详情。 我引用了两个表mdiaries和tv_diaries,在两个表中我都索引了相关的列,在mdiaries表中,tv_diaries中有2661331行和27074645行。

mdiaries table:

  INDEX area (area),
  INDEX date (date),
  INDEX district (district),
  INDEX gaDivision (gaDivision),
  INDEX member_id (member_id),
  INDEX tv_channel_id (tv_channel_id),

tv_diaries。

  INDEX area (area),
  INDEX date (date),
  INDEX district (district),
  INDEX member_id (member_id),
  INDEX timeslot_id (timeslot_id),
  INDEX tv_channel_id (tv_channel_id),

这是我的查询,需要122秒才能执行。

$sql = "SELECT COUNT(TvDiary.id) AS m_count,TvDiary.date,TvDiary.timeslot_id,TvDiary.tv_channel_id,TvDiary.district,TvDiary.area
FROM `mdiaries` AS Mdiary INNER JOIN `tv_diaries` AS TvDiary ON Mdiary.member_id = TvDiary.member_id
WHERE Mdiary.date >= '2014-01-01' AND Mdiary.date <= '2014-01-07'
AND TvDiary.date >= '2014-01-01' AND TvDiary.date <= '2014-01-07'
GROUP BY TvDiary.date,
TvDiary.timeslot_id,
TvDiary.tv_channel_id,
TvDiary.district,
TvDiary.area";

这是my.cnf文件。

    [mysqld]

## General
datadir                         = /var/lib/mysql
tmpdir                          = /var/lib/mysqltmp
socket                          = /var/lib/mysql/mysql.sock
skip-name-resolve
sql-mode                        = NO_ENGINE_SUBSTITUTION
#event-scheduler                = 1

## Networking
back-log                        = 100
#max-connections                = 200
max-connect-errors              = 10000
max-allowed-packet              = 32M
interactive-timeout             = 3600
wait-timeout                    = 600

### Storage Engines
#default-storage-engine         = InnoDB
innodb                          = FORCE

## MyISAM
key-buffer-size                 = 64M
myisam-sort-buffer-size         = 128M

## InnoDB
innodb-buffer-pool-size        = 16G
innodb_buffer_pool_instances    = 16
#innodb-log-file-size           = 100M
#innodb-log-buffer-size         = 8M
#innodb-file-per-table          = 1
#innodb-open-files              = 300

## Replication
server-id                       = 1
#log-bin                        = /var/log/mysql/bin-log
#relay-log                      = /var/log/mysql/relay-log
relay-log-space-limit           = 16G
expire-logs-days                = 7
#read-only                      = 1
#sync-binlog                    = 1
#log-slave-updates              = 1
#binlog-format                  = STATEMENT
#auto-increment-offset          = 1
#auto-increment-increment       = 2

## Logging
log-output                      = FILE
slow-query-log                  = 1
slow-query-log-file             = /var/log/mysql/slow-log
#log-slow-slave-statements
long-query-time                 = 2

##
query_cache_size        = 512M
query_cache_type        = 1
query_cache_limit       = 2M
join_buffer_size        = 512M
thread_cache_size       = 128

[mysqld_safe]
log-error                       = /var/log/mysqld.log
open-files-limit                = 65535

[mysql]
no-auto-rehash

4 个答案:

答案 0 :(得分:2)

这是您的查询:

SELECT COUNT(t.id) AS m_count, t.date, t.timeslot_id, t.tv_channel_id,
       t.district, t.area
FROM `mdiaries` m INNER JOIN
     `tv_diaries` t
     ON m.member_id = t.member_id
WHERE m.date >= '2014-01-01' AND m.date <= '2014-01-07' AND
      t.date >= '2014-01-01' AND t.date <= '2014-01-07'
GROUP BY t.date, t.timeslot_id, t.tv_channel_id, t.district, t.area;

我将从复合索引开始:tv_diaries(date, member_id)mdiaries(member_id, date)

此查询存在问题,但这些可能有所帮助。

答案 1 :(得分:0)

尝试在GROUP BY子句中引用的所有列上添加多列索引,如上所述in the documentation

INDEX grp (date, timeslot_id, tv_channel_id, district, area)

答案 2 :(得分:0)

不确定,但它可以为您提供更好的性能 -

SELECT COUNT(t.id) AS m_count, t.date, t.timeslot_id, t.tv_channel_id, t.district, t.area
FROM `mdiaries` m 
JOIN 
(
SELECT t.id, t.date, t.timeslot_id, t.tv_channel_id, t.district, t.area, t.member_id 
FROM `tv_diaries` AS t
WHERE t.date >= '2014-01-01' AND t.date <= '2014-01-07' 
) t ON m.member_id = t.member_id
WHERE m.date >= '2014-01-01' AND m.date <= '2014-01-07' 
GROUP BY t.date, t.timeslot_id, t.tv_channel_id, t.district, t.area;

您还可以检查您的数据库配置设置,因为我看到以下问题 -

  1. 对innodb_file_per_table = 1进行了评论:如果为真,那么数据将存储在单个ibd文件中而不是表格中。

  2. tmp_table_size和max_heap_table_size可以在您尝试从繁重的表中获取数据时提高性能。因此,如果您的查询在磁盘上创建临时表,请尝试将它们都设置为至少100M,以避免在磁盘上创建临时表。

  3. 因为您正在使用group by,所以sort_buffer_size变量可以帮助您增加它。可以设置2M。

  4. join_buffer_size太高它应该接近2M左右可以设置最大值。 8M但不是512M,因为它使用了会话,因此请尽量记忆。

  5. 你也将query_cache_size设置为512M,所以从这里释放内存,你也可以通过mysqltuner报告检查实际上你是否正在获得缓存查询的好处,如果没有,那么你可以禁用它。

答案 3 :(得分:0)

也许您可以使用物化视图来存储查询结果并定期刷新(每月?15天?)

这不会优化您的查询,但您的咨询会更快(不会再次计算)