简体   繁体   English

MySQL-索引会加快我的搜索查询速度吗?

[英]MySQL - will indexes speed up my search queries?

I have a table for patients in a hospital, so potentially over time this table could contain tens of thousands of rows. 我在医院有一张病人用的桌子,所以随着时间的流逝,这张桌子可能包含成千上万的行。 A hospital-unique ID number (BIGINT) is assigned to each patient which serves as the PRIMARY KEY, and the rest of the columns are VARCHAR or CHAR, and a single DATE column. 为每个患者分配一个医院唯一的ID号(BIGINT),用作主键,其余各列为VARCHAR或CHAR,以及一个DATE列。 As far as I know, PRIMARY KEYS are automatically indexed so searching by the patient's ID is optimal(?). 据我所知,PRIMARY KEYS是自动索引的,因此按患者ID进行搜索是最佳的(?)。

Patients can be searched by a single field or a combination of this ID, name, address, date of birth etc. Can I optimize my search simply by creating an INDEX on each one of these fields? 可以通过单个字段或此ID,名称,地址,出生日期等的组合来搜索患者。是否可以仅通过在每个字段上创建一个INDEX来优化我的搜索? Would this optimization also work when a search is performed on a combination of these INDEXed fields such as 当对这些INDEXed字段(例如,

 SELECT * FROM patients WHERE first_name="John" AND address="blah blah blah";

??? ???

Also, I presume MySQL uses its own internal algorithms for searching, perhaps binary search, and users cannot choose or implement their own? 另外,我认为MySQL使用自己的内部算法进行搜索(也许是二进制搜索),并且用户无法选择或实现自己的算法?

From MySQL docs http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html which is a pretty interesting read anyway: 从MySQL文档http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html开始 ,无论如何这是一个非常有趣的阅读:

Suppose that you issue the following SELECT statement: 假设您发出以下SELECT语句:

mysql> SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2; mysql> SELECT * FROM tbl_name WHERE col1 = val1和col2 = val2;

If a multiple-column index exists on col1 and col2, the appropriate rows can be fetched directly. 如果col1和col2上存在多列索引,则可以直接获取适当的行。 If separate single-column indexes exist on col1 and col2, the optimizer will attempt to use the Index Merge optimization (see 如果col1和col2上存在单独的单列索引,则优化器将尝试使用索引合并优化(请参见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM