我有这个问题:
SELECT DISTINCT
component_tbl.meta,
component_tbl.enabled,
component_tbl.id,
component_tbl.name,
component_tbl.parent,
component_tbl.option,
component_tbl.ordering,
component_tbl.iscore
FROM
components AS `component_tbl`
LEFT JOIN
nodes AS `assignment` ON assignment.component = component_tbl.option AND assignment.type LIKE 'ComComponentsDomainEntityAssignment,com:components.domain.entity.assignment'
WHERE
(
component_tbl.parent = 0 AND component_tbl.enabled = 1 AND component_tbl.id NOT IN(
SELECT
assignment.id
FROM
nodes AS `assignment`
WHERE
(
assignment.type LIKE 'ComComponentsDomainEntityAssignment,com:components.domain.entity.assignment'
) AND(
assignment.name = 'com:people.domain.entity.person' AND assignment.access = 2
)
) AND(
(
assignment.name = 'com:people.domain.entity.person' AND access = 1
) OR assignment.owner_id = NULL OR component_tbl.option = "com_stories"
)
)
并且它在我当前使用MySQL 5.5.52的服务器上运行良好,但我目前正在使用MySQL 5.7.20升级到新服务器并且速度要慢得多。在5.5.52,它大约是0.3秒,在5.7.20大约是4秒。所以我在两台服务器上都做了一个EXPLAIN:
MySQL 5.5.52:
+----+--------------------+---------------+-----------------+--------------------------------------------------------------------------------------------------+---------+---------+------+------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+---------------+-----------------+--------------------------------------------------------------------------------------------------+---------+---------+------+------+------------------------------+
| 1 | PRIMARY | component_tbl | ALL | parent_option | NULL | NULL | NULL | 27 | Using where; Using temporary |
| 1 | PRIMARY | assignment | range | type,component,type_enabled,type_modifed_on,type_created_on,type_status_update_time,type_default | type | 767 | NULL | 697 | Using where; Distinct |
| 2 | DEPENDENT SUBQUERY | assignment | unique_subquery | PRIMARY,type,type_enabled,type_modifed_on,type_created_on,type_status_update_time,type_default | PRIMARY | 8 | func | 1 | Using where |
+----+--------------------+---------------+-----------------+--------------------------------------------------------------------------------------------------+---------+---------+------+------+------------------------------+
MySQL 5.7.20
+----+--------------------+---------------+------------+-----------------+---------------------------------------------------------------------------------------------------+---------+---------+------+-------+----------+--------------------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------------+---------------+------------+-----------------+---------------------------------------------------------------------------------------------------+---------+---------+------+-------+----------+--------------------------------------------------------------+
| 1 | PRIMARY | component_tbl | NULL | ALL | parent_option | NULL | NULL | NULL | 25 | 10.00 | Using where; Using temporary |
| 1 | PRIMARY | assignment | NULL | ALL | type,component,type_enabled,type_modifed_on,type_created_on,type_status_update_time,type_default | NULL | NULL | NULL | 98853 | 100.00 | Range checked for each record (index map: 0xF8060); Distinct |
| 2 | DEPENDENT SUBQUERY | assignment | NULL | unique_subquery | PRIMARY,id,type,type_enabled,type_modifed_on,type_created_on,type_status_update_time,type_default | PRIMARY | 8 | func | 1 | 5.00 | Using where |
+----+--------------------+---------------+------------+-----------------+---------------------------------------------------------------------------------------------------+---------+---------+------+-------+----------+--------------------------------------------------------------+
如果你比较两者,你会在第二个查询中看到赋值选择,它在一个中使用type
索引,而在另一个中使用INDEXES
索引。两个表的+-----------+------------+-------------------------+--------------+----------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+-------------------------+--------------+----------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| nodes | 0 | PRIMARY | 1 | id | A | 74387 | NULL | NULL | | BTREE | | |
| nodes | 0 | person_username | 1 | person_username | A | 18596 | NULL | NULL | YES | BTREE | | |
| nodes | 0 | person_userid | 1 | person_userid | A | 2 | NULL | NULL | YES | BTREE | | |
| nodes | 0 | person_useremail | 1 | person_useremail | A | 2 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | last_comment_by | 1 | last_comment_by | A | 2 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | created_by | 1 | created_by | A | 3915 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | modified_by | 1 | modified_by | A | 2755 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | person_lastvisitdate | 1 | person_lastvisitdate | A | 12397 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | type | 1 | type | A | 11 | NULL | NULL | | BTREE | | |
| nodes | 1 | component | 1 | component | A | 34 | NULL | NULL | | BTREE | | |
| nodes | 1 | owner_id | 1 | owner_id | A | 2010 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | parent_id | 1 | parent_id | A | 18596 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | story_target_id | 1 | story_target_id | A | 312 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | story_object_id | 1 | story_object_id | A | 74387 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | story_subject_id | 1 | story_subject_id | A | 1352 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | story_comment_id | 1 | story_comment_id | A | 2 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | start_date | 1 | start_date | A | 2 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | end_date | 1 | end_date | A | 2 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | type_enabled | 1 | type | A | 422 | NULL | NULL | | BTREE | | |
| nodes | 1 | type_enabled | 2 | enabled | A | 630 | NULL | NULL | | BTREE | | |
| nodes | 1 | type_modifed_on | 1 | type | A | 12 | NULL | NULL | | BTREE | | |
| nodes | 1 | type_modifed_on | 2 | modified_on | A | 74387 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | type_created_on | 1 | type | A | 12 | NULL | NULL | | BTREE | | |
| nodes | 1 | type_created_on | 2 | created_on | A | 74387 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | type_status_update_time | 1 | type | A | 11 | NULL | NULL | | BTREE | | |
| nodes | 1 | type_status_update_time | 2 | status_update_time | A | 11 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | type_default | 1 | type | A | 11 | NULL | NULL | | BTREE | | |
| nodes | 1 | type_default | 2 | is_default | A | 11 | NULL | NULL | | BTREE | | |
+-----------+------------+-------------------------+--------------+----------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
大致相同。
MySQL 5.5.52
+-----------+------------+-------------------------+--------------+--------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+-------------------------+--------------+--------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| nodes | 0 | PRIMARY | 1 | id | A | 98853 | NULL | NULL | | BTREE | | |
| nodes | 0 | id | 1 | id | A | 98853 | NULL | NULL | | BTREE | | |
| nodes | 1 | last_comment_by | 1 | last_comment_by | A | 735 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | created_by | 1 | created_by | A | 2157 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | modified_by | 1 | modified_by | A | 2182 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | type | 1 | type | A | 24 | NULL | NULL | | BTREE | | |
| nodes | 1 | component | 1 | component | A | 12 | NULL | NULL | | BTREE | | |
| nodes | 1 | owner_id | 1 | owner_id | A | 1346 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | parent_id | 1 | parent_id | A | 8868 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | story_target_id | 1 | story_target_id | A | 999 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | story_object_id | 1 | story_object_id | A | 11165 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | story_subject_id | 1 | story_subject_id | A | 743 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | story_comment_id | 1 | story_comment_id | A | 2329 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | start_date | 1 | start_date | A | 1 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | end_date | 1 | end_date | A | 1 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | type_enabled | 1 | type | A | 24 | NULL | NULL | | BTREE | | |
| nodes | 1 | type_enabled | 2 | enabled | A | 28 | NULL | NULL | | BTREE | | |
| nodes | 1 | type_modifed_on | 1 | type | A | 24 | NULL | NULL | | BTREE | | |
| nodes | 1 | type_modifed_on | 2 | modified_on | A | 98853 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | type_created_on | 1 | type | A | 24 | NULL | NULL | | BTREE | | |
| nodes | 1 | type_created_on | 2 | created_on | A | 98853 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | type_status_update_time | 1 | type | A | 24 | NULL | NULL | | BTREE | | |
| nodes | 1 | type_status_update_time | 2 | status_update_time | A | 24 | NULL | NULL | YES | BTREE | | |
| nodes | 1 | type_default | 1 | type | A | 24 | NULL | NULL | | BTREE | | |
| nodes | 1 | type_default | 2 | is_default | A | 24 | NULL | NULL | | BTREE | | |
+-----------+------------+-------------------------+--------------+--------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
MySQL 5.7.20
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <stddef.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
bool kill_self = false;
void my_signal_handler(int signum, siginfo_t* info, void* ptr){
// register action for SIGINT (find out my pid and kill self if kill_self)
printf("Signal sent from process %lu\n", (unsigned long)info->si_pid);
if (kill_self){ //kill_self will surely be updated correctly?
kill(getpid(), SIGKILL); //return int - for check?
}
}
int prepare(){
struct sigaction new_action;
memset(&new_action, 0, sizeof(new_action));
new_action.sa_sigaction = my_signal_handler;
new_action.sa_flags = SA_SIGINFO;
//register handler
if (0 != sigaction(SIGINT, &new_action, NULL)){
printf("Signal handler registration %s\n failed", strerror(errno));
return 1;
}
return 0;
}
int main(void)
{
if (prepare() != 0)
exit(-1);
while (1)
{
char** arglist = NULL;
char* line = NULL;
size_t size;
int count = 0;
if (getline(&line, &size, stdin) == -1) {
printf("getline failed\n");
free(line);
break;
}
arglist = (char**) malloc(sizeof(char*));
if (arglist == NULL) {
printf("malloc failed: %s\n", strerror(errno));
exit(-1);
}
arglist[0] = strtok(line, " \t\n");
while (arglist[count] != NULL) {
printf("in while\n");
++count;
arglist = (char**) realloc(arglist, sizeof(char*) * (count + 1));
if (arglist == NULL) {
printf("realloc failed: %s\n", strerror(errno));
exit(-1);
}
arglist[count] = strtok(NULL, " \t\n");
}
//continue code...
迁移过程中对表格进行了一些更改,但没有太多戏剧性。 MySQL中是否会发生变化导致功能发生这种变化?有没有更好的方法来优化此查询?
我不知道为什么索引似乎停止工作,我很欣赏任何输入。谢谢!
答案 0 :(得分:1)
只看索引:
INDEX(type)
vs INDEX(type, enabled)
)。UNIQUE
个键?这是非常不寻常的,可能&#34;错误&#34;。这是错误的错误:assignment.owner_id = NULL
- 它始终为FALSE,因为NULL
永远不会等于任何内容,甚至NULL
。您需要assignment.owner_id IS NULL
。
代替NOT IN ( SELECT ... )
,执行NOT EXISTS ( SELECT * ... )
或LEFT JOIN ... WHERE ... IS NULL
。
虽然可以说type LIKE 'ComComponentsDomainEntityAssignment,com:components.domain.entity.assignment'
,但最好说type = 'ComComponentsDomainEntityAssignment,com:components.domain.entity.assignment'
nodes AS assignment
- 对两个不同的实例使用相同的别名有点令人困惑。
有些括号是多余的。
在我尝试继续提问之前,请先解决一些问题。