如何让MySQL利用可用的系统资源,或找到"真正的问题"?

ano*_*ard 7 mysql

这是在SuSE Enterprise 10上运行的MySQL 5.0.26服务器.这可能是Serverfault问题.

使用这些特定查询的Web用户界面(如下所示)有时会显示30+,甚至最差120+ ,以生成所涉及的页面.

在开发时,当查询单独运行时,它们在第一次运行时最多需要20秒(没有启用查询缓存)但在此之后的2到7秒内 - 我假设因为所涉及的表和索引已放入ram .

据我所知,最长的加载时间是由读/更新锁定引起的.这些是MyISAM表.所以它似乎是一个长时间的更新,然后是几秒钟的查询,他们只是加起来.我对这个解释很好.

我不喜欢的是MySQL似乎没有使用它所使用的硬件,虽然瓶颈似乎是数据库,但我无法理解为什么.

我会说"扔更多的硬件",但我们做了,似乎并没有改变这种情况.在最慢的时间内查看"顶部"从来没有显示出太多的CPU或内存利用率mysqld,好像服务器完全没有问题 - 但是,为什么查询需要这么长时间?

我怎样才能让MySQL使用这个硬件的废话,或者找出我做错了什么?

额外细节:

在MySQL Administrator(对于Windows)的"内存运行状况"选项卡中,密钥缓冲区使用的次数少于1/8 - 因此所有索引都应该在RAM中.我可以提供任何可能有用的图表的屏幕截图.

所以急切地想解决这个问题.可以这么说,有遗留代码"生成"这些查询,而且它们几乎就像它们一样.我已经尝试了所涉及的表的每个索引组合,但欢迎任何建议.

这是开发中的当前Create Table语句(我添加的'实验'键,对于示例查询,似乎有点帮助):

CREATE TABLE `registration_task` (                                                       
    `id` varchar(36) NOT NULL default '',                                                  
    `date_entered` datetime NOT NULL default '0000-00-00 00:00:00',                        
    `date_modified` datetime NOT NULL default '0000-00-00 00:00:00',                       
    `assigned_user_id` varchar(36) default NULL,                                           
    `modified_user_id` varchar(36) default NULL,                                           
    `created_by` varchar(36) default NULL,                                                 
    `name` varchar(80) NOT NULL default '',                                                
    `status` varchar(255) default NULL,                                                    
    `date_due` date default NULL,                                                          
    `time_due` time default NULL,                                                          
    `date_start` date default NULL,                                                        
    `time_start` time default NULL,                                                        
    `parent_id` varchar(36) NOT NULL default '',                                           
    `priority` varchar(255) NOT NULL default '9',                                          
    `description` text,                                                                    
    `order_number` int(11) default '1',                                                    
    `task_number` int(11) default NULL,                                                    
    `depends_on_id` varchar(36) default NULL,                                              
    `milestone_flag` varchar(255) default NULL,                                            
    `estimated_effort` int(11) default NULL,                                               
    `actual_effort` int(11) default NULL,                                                  
    `utilization` int(11) default '100',                                                   
    `percent_complete` int(11) default '0',                                                
    `deleted` tinyint(1) NOT NULL default '0',                                             
    `wf_task_id` varchar(36) default '0',                                                  
    `reg_field` varchar(8) default '',                                                     
    `date_offset` int(11) default '0',                                                     
    `date_source` varchar(10) default '',                                                  
    `date_completed` date default '0000-00-00',                                            
    `completed_id` varchar(36) default NULL,                                               
    `original_name` varchar(80) default NULL,                                              
    PRIMARY KEY  (`id`),                                                                   
    KEY `idx_reg_task_p` (`deleted`,`parent_id`),                                          
    KEY `By_Assignee` (`assigned_user_id`,`deleted`),                                      
    KEY `status_assignee` (`status`,`deleted`),                                            
    KEY `experimental` (`deleted`,`status`,`assigned_user_id`,`parent_id`,`date_due`)  
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1   
Run Code Online (Sandbox Code Playgroud)

还有一个有问题的荒谬查询:

SELECT 
    users.user_name   
    assigned_user_name, 
    registration.FIELD001 parent_name, 
    registration_task.status status,
    registration_task.date_modified date_modified,
    registration_task.date_due date_due, 
    registration.FIELD240 assigned_wf,
    if(LENGTH(registration_task.description)>0,1,0) has_description,
    registration_task.* 
FROM 
    registration_task LEFT JOIN users ON registration_task.assigned_user_id=users.id 
    LEFT JOIN registration ON registration_task.parent_id=registration.id 
where 
    (registration_task.status != 'Completed' AND registration.FIELD001 LIKE '%' 
       AND registration_task.name LIKE '%' AND registration.FIELD060 LIKE 'GN001472%') 
    AND  registration_task.deleted=0  
ORDER BY date_due asc LIMIT 0,20;
Run Code Online (Sandbox Code Playgroud)

my.cnf - '[mysqld]'部分.

[mysqld]
port = 3306
socket  = /var/lib/mysql/mysql.sock
skip-locking
key_buffer = 384M
max_allowed_packet = 100M
table_cache = 2048 
sort_buffer_size = 2M 
net_buffer_length = 100M 
read_buffer_size = 2M 
read_rnd_buffer_size = 160M 
myisam_sort_buffer_size = 128M
query_cache_size = 16M
query_cache_limit = 1M
Run Code Online (Sandbox Code Playgroud)

EXPLAIN上面的查询,没有附加索引

:

       
+----+-------------+-------------------+--------+--------------------------------+----------------+---------+------------------------------------------------+---------+-----------------------------+
| id | select_type | table             | type   | possible_keys                  | key            | key_len | ref                                            | rows    | Extra                       |
+----+-------------+-------------------+--------+--------------------------------+----------------+---------+------------------------------------------------+---------+-----------------------------+
|  1 | SIMPLE      | registration_task | ref    | idx_reg_task_p,status_assignee | idx_reg_task_p | 1       | const                                          | 1067354 | Using where; Using filesort |
|  1 | SIMPLE      | registration      | eq_ref | PRIMARY,gbl                    | PRIMARY        | 8       | sugarcrm401.registration_task.parent_id        |       1 | Using where                 |
|  1 | SIMPLE      | users             | ref    | PRIMARY                        | PRIMARY        | 38      | sugarcrm401.registration_task.assigned_user_id |       1 |                             |
+----+-------------+-------------------+--------+--------------------------------+----------------+---------+------------------------------------------------+---------+-----------------------------+

使用'experimental'索引解析上面的查询:

       
+----+-------------+-------------------+--------+-----------------------------------------------------------+------------------+---------+------------------------------------------------+--------+-----------------------------+
| id | select_type | table             | type   | possible_keys                                             | key              | key_len | ref                                            | rows   | Extra                       |
+----+-------------+-------------------+--------+-----------------------------------------------------------+------------------+---------+------------------------------------------------+--------+-----------------------------+
|  1 | SIMPLE      | registration_task | range  | idx_reg_task_p,status_assignee,NewIndex1,tcg_experimental | tcg_experimental | 259     | NULL                                           | 103345 | Using where; Using filesort |
|  1 | SIMPLE      | registration      | eq_ref | PRIMARY,gbl                                               | PRIMARY          | 8       | sugarcrm401.registration_task.parent_id        |      1 | Using where                 |
|  1 | SIMPLE      | users             | ref    | PRIMARY                                                   | PRIMARY          | 38      | sugarcrm401.registration_task.assigned_user_id |      1 |                             |
+----+-------------+-------------------+--------+-----------------------------------------------------------+------------------+---------+------------------------------------------------+--------+-----------------------------+

来自registration_task的SHOW INDEXES;

mysql> SHOW INDEXES FROM registration_task;
+-------------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+
| Table             | Non_unique | Key_name         | Seq_in_index | Column_name      | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+
| registration_task |          0 | PRIMARY          |            1 | id               | A         |     1445612 |     NULL | NULL   |      | BTREE      |         |
| registration_task |          1 | idx_reg_task_p   |            1 | deleted          | A         |           2 |     NULL | NULL   |      | BTREE      |         |
| registration_task |          1 | idx_reg_task_p   |            2 | parent_id        | A         |       57824 |     NULL | NULL   |      | BTREE      |         |
| registration_task |          1 | By_Assignee      |            1 | assigned_user_id | A         |        5295 |     NULL | NULL   | YES  | BTREE      |         |
| registration_task |          1 | By_Assignee      |            2 | deleted          | A         |        5334 |     NULL | NULL   |      | BTREE      |         |
| registration_task |          1 | status_assignee  |            1 | status           | A         |          18 |     NULL | NULL   | YES  | BTREE      |         |
| registration_task |          1 | status_assignee  |            2 | deleted          | A         |          23 |     NULL | NULL   |      | BTREE      |         |
| registration_task |          1 | NewIndex1        |            1 | deleted          | A         |           2 |     NULL | NULL   |      | BTREE      |         |
| registration_task |          1 | NewIndex1        |            2 | assigned_user_id | A         |        5334 |     NULL | NULL   | YES  | BTREE      |         |
| registration_task |          1 | NewIndex1        |            3 | parent_id        | A         |      180701 |     NULL | NULL   |      | BTREE      |         |
| registration_task |          1 | tcg_experimental |            1 | date_due         | A         |        1919 |     NULL | NULL   | YES  | BTREE      |         |
| registration_task |          1 | tcg_experimental |            2 | deleted          | A         |        3191 |     NULL | NULL   |      | BTREE      |         |
| registration_task |          1 | tcg_experimental |            3 | status           | A         |        8503 |     NULL | NULL   | YES  | BTREE      |         |
| registration_task |          1 | tcg_experimental |            4 | assigned_user_id | A         |       53541 |     NULL | NULL   | YES  | BTREE      |         |
| registration_task |          1 | tcg_experimental |            5 | parent_id        | A         |      722806 |     NULL | NULL   |      | BTREE      |         |
+-------------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+

15 rows in set (0.00 sec)

解?

我想我可能已经解决了这个问题,对某些人来说这似乎是如此令人尴尬的显而易见,但直到现在却被忽略了:定义registration.id是:

`id` bigint(20) unsigned NOT NULL auto_increment
Run Code Online (Sandbox Code Playgroud)

虽然registration_task.parent_id(FK to registration.id)是:

`parent_id` varchar(36) NOT NULL
Run Code Online (Sandbox Code Playgroud)

改变这个:

alter table `sugarcrm401`.`registration_task` change `parent_id` `parent_id` bigint(20) UNSIGNED NOT NULL;
Run Code Online (Sandbox Code Playgroud)

...导致EXPLAIN,只显示25行检查,在此前有651903103345在迫使它疯狂的索引时是最好的.

如果我发布了表的表定义registration,我相信有人可能已经发现了它.我将在周末之后验证这一点并发布后续信息.

ano*_*ard 0

大多数问题的答案已在我的编辑中发布:

的定义registration.id是:

`id` bigint(20) unsigned NOT NULL auto_increment
Run Code Online (Sandbox Code Playgroud)

registration_task.parent_id(FK 到registration.id) 是:

`parent_id` varchar(36) NOT NULL
Run Code Online (Sandbox Code Playgroud)

通过以下方式更改此设置:

alter table `sugarcrm401`.`registration_task` change `parent_id` `parent_id` bigint(20) UNSIGNED NOT NULL;
Run Code Online (Sandbox Code Playgroud)

...导致 EXPLAIN 仅显示检查的25行,之前是651,903,在强制疯狂索引时最好显示103,345 。

如果我发布了该表的表定义registration,我相信有人可能会发现它。


然而,这并没有完全解决问题。我确信我遗漏了一些细节,但这个项目目前已经远远落后于我了。非常感谢您的所有时间和回答!