[英]How to improve search performance in MySQL
I have a table that contains two bigint
columns: beginNumber
, endNumber
, defined as UNIQUE
. 我有一个包含两个
bigint
列的表: beginNumber
, endNumber
,定义为UNIQUE
。 The ID
is the Primary Key
. 该
ID
是Primary Key
。
ID | beginNumber | endNumber | Name | Criteria
The second table contains a number. 第二个表包含一个数字。 I want to retrieve the record from table1 when the Number from table2 is found to be between any two numbers.
当发现table2中的数字位于任意两个数字之间时,我想从table1中检索记录。 The is the query:
是查询:
select distinct t1.Name, t1.Country
from t1
where t2.Number
BETWEEN t1.beginIpNum AND t1.endNumber
The query is taking too much time as I have so many records. 由于我有很多记录,因此查询花费了太多时间。 I don't have experience in DB.
我没有DB的经验。 But, I read that indexing the table will improve the search so MySQL does not have to pass through every row searching about m Number and this can be done by, for example, having
UNIQE
values. 但是,我读到对表建立索引将改善搜索,因此MySQL不必遍历每行有关m Number的搜索,这可以通过例如具有
UNIQE
值来完成。 I made the beginNumber
& endNumber
in table1 as UNIQUE. 我在表1中将
beginNumber
和endNumber
设为UNIQUE。 Is this all what I can do ? 这就是我所能做的吗? Is there any possible way to improve the time ?
有什么办法可以缩短时间? Please, provide detailed answers.
请提供详细的答案。
EDIT: 编辑:
table1: 表格1:
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`beginNumber` bigint(20) DEFAULT NULL,
`endNumber` bigint(20) DEFAULT NULL,
`Name` varchar(255) DEFAULT NULL,
`Criteria` varchar(455) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `beginNumber_UNIQUE` (`beginNumber`),
UNIQUE KEY `endNumber_UNIQUE` (`endNumber `)
) ENGINE=InnoDB AUTO_INCREMENT=327 DEFAULT CHARSET=utf8
table2: 表2:
CREATE TABLE `t2` (
`id2` int(11) NOT NULL AUTO_INCREMENT,
`description` varchar(255) DEFAULT NULL,
`Number` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id2`),
UNIQUE KEY ` description _UNIQUE` (`description `)
) ENGINE=InnoDB AUTO_INCREMENT=433 DEFAULT CHARSET=utf8
This is a toy example of the tables but it shows the concerned part. 这是桌子的玩具示例,但显示了相关部分。
I'd suggest an index on t2.Number
like this: 我建议在
t2.Number
上t2.Number
一个索引,如下所示:
ALTER TABLE t2 ADD INDEX numindex(Number);
Your query won't work as written because it won't know which t2 to use. 您的查询无法按书面要求运行,因为它不知道要使用哪个t2。 Try this:
尝试这个:
SELECT DISTINCT t1.Name, t1.Criteria
FROM t1
WHERE EXISTS (SELECT * FROM t2 WHERE t2.Number BETWEEN t1.beginNumber AND t1.endNumber);
Without the t2.Number index EXPLAIN gives this query plan: 没有t2.Number索引,EXPLAIN给出以下查询计划:
1 PRIMARY t1 ALL 1 Using where; Using temporary
2 DEPENDENT SUBQUERY t2 ALL 1 Using where
With an index on t2.Number, you get this plan: 使用t2.Number的索引,您可以获得以下计划:
PRIMARY t1 ALL 1 Using where; Using temporary
DEPENDENT SUBQUERY t2 index numindex numindex 9 1 Using where; Using index
The important part to understand is that an ALL
comparison is slower than an index
comparison. 要理解的重要部分是,
ALL
比较比index
比较慢。
This is a good place to use binary tree index (default is hashmap). 这是使用二叉树索引的好地方(默认为hashmap)。 Btree indexes are best when you often sort or use between on column.
当您经常在列之间进行排序或使用时,Btree索引是最好的。
CREATE INDEX index_name 创建索引index_name
ON table_name (column_name) ON table_name(column_name)
USING BTREE 使用BTREE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.