在我的慢查询日志中,我看到慢查询,例如
# 时间:121107 16:34:02 # 用户@主机:web_node[web_node]@localhost [127.0.0.1] # Thread_id: 34436186 架构: test_db Last_errno: 0 Killed: 0 # Query_time: 1.413751 Lock_time: 0.000222 Rows_sent: 203 Rows_examined: 203 Rows_affected: 0 Rows_read: 203 # Bytes_sent: 7553 Tmp_tables: 0 Tmp_disk_tables: 0 Tmp_table_sizes: 0 # InnoDB_trx_id: 9B04384 设置时间戳=1352334842; SELECT ID,电子邮件,TEST_DATA WHERE ID IN(13089576,3002681,3117763,1622233,2941590,12305279,1732672,2446772,3189510,13084725,4943929,5855071,6572137,2266261,3003496,2024860,3336832,13758671,6477694,1796684, 13001771,4690025,1071744,1017876,5175795,795988,1619821,2481819,2941090,4770802,13438250,3254708,2323402,526303,13219855,3313573,3190479,1733761,3300577,2941758,6474118,1733379,11523598,4205064,6521805, 249290311111111198055050580588055805580558058805588055805585055850558505850558058505880558505850558805585058505850585055805850558058505880558055880585055 1338572,7272410,1860386,2567550,5434143,1918035,5329411,1683235,3254119,5175784,1855380,3336834,2102567,4749746,37269,3207031,6464336,2227907,2713471,3937600,2940442,2233821,5619141,5204711,5988803, 5050821,10109926,5226877,5050275,1874115,13677832,5338699,2423773,6432937,6443660,1990611,6090667,6527411,6568731,3254846,3414049,2011907,5180984,12178711,8558260,3130655,5864745,2059318,3480233,2104948,2387703,1939395,5356002,2681209,1184622, 1184456,10390165,510854,7983305,795991,2622393,4490187,9436477,5356051,2423464,5205318,1600499,13623229,3255205,12200483,6477706,3445661,5226284,1176639,13760962,2101681,6022818,12909371,1732457,2377496, 7260091,12191702,2492899,2630691,13047691,1684470,9382108,2233737,13117701,1796698,2535914,4941741,4565958,1100410,2321180,13080467,813342,4563877,4689365,2104756,1102802,2714488,3188947,1599770,1558291, 5592740,5233428,5204830,1574452,3188956,13693326,2102349,3704111,1748303,790889,9323280,4741494,2387900,5338213,3583795,2283942,3189482,3002296,4490123,3585020,962926,3481423,1600920,1682364,4693123, 6487778,2677582,2377195);6443660,1990611,6090667,6527411,6568731,3254846,3414049,2011907,5180984,12178711,8558260,3130655,5864745,2059318,3480233,2104948,2387703,1939395,5356002,2681209,1184622,1184456,10390165,510854,7983305, 795991,2622393,4490187,9436477,5356051,2423464,5205318,1600499,13623229,3255205,12200483,6477706,3445661,5226284,1176639,13760962,2101681,6022818,12909371,1732457,2377496,7260091,12191702,2492899,2630691, 13047691,1684470,9382108,2233737,13117701,1796698,2535914,4941741,4565958,1100410,2321180,13080467,813342,4563877,4689365,2104756,1102802,2714488,3188947,1599770,1558291,5592740,5233428,5204830,1574452, 3188956,13693326,2102349,3704111,1748303,790889,9323280,4741494,2387900,5338213,3583795,2283942,3189482,3002296,4490123,3585020,962926,3481423,1600920,1682364,4693123,6487778,2677582,2377195);6443660,1990611,6090667,6527411,6568731,3254846,3414049,2011907,5180984,12178711,8558260,3130655,5864745,2059318,3480233,2104948,2387703,1939395,5356002,2681209,1184622,1184456,10390165,510854,7983305, 795991,2622393,4490187,9436477,5356051,2423464,5205318,1600499,13623229,3255205,12200483,6477706,3445661,5226284,1176639,13760962,2101681,6022818,12909371,1732457,2377496,7260091,12191702,2492899,2630691, 13047691,1684470,9382108,2233737,13117701,1796698,2535914,4941741,4565958,1100410,2321180,13080467,813342,4563877,4689365,2104756,1102802,2714488,3188947,1599770,1558291,5592740,5233428,5204830,1574452, 3188956,13693326,2102349,3704111,1748303,790889,9323280,4741494,2387900,5338213,3583795,2283942,3189482,3002296,4490123,3585020,962926,3481423,1600920,1682364,4693123,6487778,2677582,2377195);2011907,5180984,12178711,8558260,3130655,5864745,2059318,3480233,2104948,2387703,1939395,5356002,2681209,1184622,1184456,10390165,510854,7983305,795991,2622393,4490187,9436477,5356051,2423464,5205318, 1600499,13623229,3255205,12200483,6477706,3445661,5226284,1176639,13760962,2101681,6022818,12909371,1732457,2377496,7260091,12191702,2492899,2630691,13047691,1684470,9382108,2233737,13117701,1796698,2535914, 4941741,4565958,1100410,2321180,13080467,813342,4563877,4689365,2104756,1102802,2714488,3188947,1599770,1558291,5592740,5233428,5204830,1574452,3188956,13693326,2102349,3704111,1748303,790889,9323280, 4741494,2387900,5338213,3583795,2283942,3189482,3002296,4490123,3585020,962926,3481423,1600920,1682364,4693123,6487378,26757521);2011907,5180984,12178711,8558260,3130655,5864745,2059318,3480233,2104948,2387703,1939395,5356002,2681209,1184622,1184456,10390165,510854,7983305,795991,2622393,4490187,9436477,5356051,2423464,5205318, 1600499,13623229,3255205,12200483,6477706,3445661,5226284,1176639,13760962,2101681,6022818,12909371,1732457,2377496,7260091,12191702,2492899,2630691,13047691,1684470,9382108,2233737,13117701,1796698,2535914, 4941741,4565958,1100410,2321180,13080467,813342,4563877,4689365,2104756,1102802,2714488,3188947,1599770,1558291,5592740,5233428,5204830,1574452,3188956,13693326,2102349,3704111,1748303,790889,9323280, 4741494,2387900,5338213,3583795,2283942,3189482,3002296,4490123,3585020,962926,3481423,1600920,1682364,4693123,6487378,26757521);10390165,510854,7983305,795991,2622393,4490187,9436477,5356051,2423464,5205318,1600499,13623229,3255205,12200483,6477706,3445661,5226284,1176639,13760962,2101681,6022818,12909371,1732457,2377496,7260091, 12191702,2492899,2630691,13047691,1684470,9382108,2233737,13117701,1796698,2535914,4941741,4565958,1100410,2321180,13080467,813342,4563877,4689365,2104756,1102802,2714488,3188947,1599770,1558291,5592740, 5233428,5204830,1574452,3188956,13693326,2102349,3704111,1748303,790889,9323280,4741494,2387900,5338213,3583795,2283942,3189482,3002296,4490123,3585020,962926,3481423,1600920,1682364,4693123,6487778, 2677582,2377195);10390165,510854,7983305,795991,2622393,4490187,9436477,5356051,2423464,5205318,1600499,13623229,3255205,12200483,6477706,3445661,5226284,1176639,13760962,2101681,6022818,12909371,1732457,2377496,7260091, 12191702,2492899,2630691,13047691,1684470,9382108,2233737,13117701,1796698,2535914,4941741,4565958,1100410,2321180,13080467,813342,4563877,4689365,2104756,1102802,2714488,3188947,1599770,1558291,5592740, 5233428,5204830,1574452,3188956,13693326,2102349,3704111,1748303,790889,9323280,4741494,2387900,5338213,3583795,2283942,3189482,3002296,4490123,3585020,962926,3481423,1600920,1682364,4693123,6487778, 2677582,2377195);1684470,9382108,2233737,13117701,1796698,2535914,4941741,4565958,1100410,2321180,13080467,813342,4563877,4689365,2104756,1102802,2714488,3188947,1599770,1558291,5592740,5233428,5204830,1574452,3188956, 136908011801118011801188011880118888888888888888885888888858888585888588885885885858858888885858588858858888588858888858858858588858585885885885858588588588585888858588858588885885885858888588588588588588858888585885885888588585888858588858588588585851684470,9382108,2233737,13117701,1796698,2535914,4941741,4565958,1100410,2321180,13080467,813342,4563877,4689365,2104756,1102802,2714488,3188947,1599770,1558291,5592740,5233428,5204830,1574452,3188956, 13690801180111801180118801188011888888888888888888588888885888858588858888588588585885888888585858885885888858885888885885885858885858588588588585858858858858588885858885858888588588585888858858858858858885888858588588588858858588885858885858858858585
当我使用 SQL_NO_CACHE 通过探查器运行慢查询时,它看起来说
一组 203 行(0.03 秒) 显示查询 33 的配置文件; +----------+----------+ | 状态 | 持续时间 | +----------+----------+ | 出发| 0.000187 | | 检查权限 | 0.000012 | | 开桌 | 0.000034 | | 系统锁 | 0.000016 | | 初始化 | 0.000087 | | 优化 | 0.000024 | | 统计 | 0.028694 | | 准备| 0.000074 | | 执行 | 0.000005 | | 发送数据 | 0.001596 | | 结束 | 0.000009 | | 查询结束 | 0.000008 | | 关闭表| 0.000014 | | 释放物品 | 0.001600 | | 记录慢查询 | 0.000007 | | 清理| 0.000011 | +----------+----------+
当我用解释运行查询时,它说
+----+-------------+------------------+--------+--- ---------------+----------+----------+------+------ +----------------------------+ | 编号 | 选择类型 | 表| 类型 | 可能的键 | 关键 | key_len | 参考 | 行 | 额外 | +----+-------------+------------------+--------+--- ---------------+----------+----------+------+------ +----------------------------+ | 1 | 简单 | 测试数据 | 范围 | 主要,id_email | id_email | 4 | 空 | 203 | 使用哪里;使用索引 | +----+-------------+------------------+--------+--- ---------------+----------+----------+------+------ +----------------------------+
创建表看起来像
创建表`test_data`( `id` int(11) NOT NULL AUTO_INCREMENT, `email` varchar(254) 默认为空, `domain` varchar(254) 默认为空, `age` smallint(6) 默认为空, `gender` tinyint(1) 默认为空, `location_id` int(11) 无符号默认 NULL, `created` 时间戳 NOT NULL DEFAULT '0000-00-00 00:00:00', `unistall_date` 时间戳 NOT NULL DEFAULT '0000-00-00 00:00:00', `subscription_date` 时间戳 NOT NULL DEFAULT '0000-00-00 00:00:00', `active` tinyint(1) 默认 '1', 主键(`id`), 唯一键`email`(`email`), KEY `domain` (`domain`), KEY `id_email` (`id`,`email`), KEY `email_id` (`email`,`id`) ) 引擎=InnoDB AUTO_INCREMENT=13848530 默认字符集=utf8
还有另一个查询会定期运行,从电子邮件地址列表中选择 ID 和电子邮件,因此电子邮件、ID 键、电子邮件地址必须是唯一的,因此为什么这是唯一键。该表只有约 14M 行
我想也许索引在内存和交换方面变得很大,但盒子有 8 gigs 的内存。
SELECT table_schema "数据库名称", SUM(data_length + index_length) / 1024 / 1024 "数据库大小 (MB)", SUM( index_length) / 1024 / 1024 "索引大小 (MB)" FROM information_schema.TABLES GROUP BY table_schema; +--------------------+----------+------ -------------+ | 数据库名称 | 以 MB 为单位的数据库大小 | 以 MB 为单位的索引大小 | +--------------------+----------+------ -------------+ | 指标 | 3192.50000000 | 1594.42187500 | | 数据 | 8096.48437500 | 5639.51562500 | | 原始数据 | 6000.35937500 | 745.07812500 | | 信息架构 | 0.00878906 | 0.00878906 | | mysql | 0.04319191 | 0.04101563 | | 性能模式 | 0.00000000 | 0.00000000 | +--------------------+----------+------ -------------+
在 my.cnf 文件中设置 innodb_file_per_table=1 似乎已经解决了这个问题。
这提高了执行时间,我的理解是每个表有一个文件意味着磁盘针不需要移动这么大的距离。
问题
- 如果可以使用索引评估查询,为什么设置 innodb_file_per_table=1 会提高性能
- 为什么在不使用缓存的情况下为探查器运行查询时查询不慢?
- 我的主键应该是 (id, email) 吗?
更新
最初没有 /etc/my.cnf 文件,然后我用以下内容创建了一个
[mysqld] 服务器 ID=1 最大连接数=1500 key_buffer_size=50M query_cache_limit=16M query_cache_size=256M 线程缓存=16 table_open_cache=4096 sort_buffer_size=512K join_buffer_size=8M read_buffer_size=8M skip_name_resolve=1 线程缓存大小=256 innodb_buffer_pool_size=6G innodb_buffer_pool_instances=1 innodb_thread_concurrency=96 innodb_additional_mem_pool_size=32M innodb_log_buffer_size=8M innodb_flush_log_at_trx_commit=0 innodb_log_file_size=256M innodb_flush_method=O_DIRECT innodb_file_per_table=1 net_read_timeout=15 net_write_timeout=30 日志-bin=mysql-bin sync_binlog=0 数据目录=/var/lib/mysql