我有一个非常大的 MySQL 表,其中包含大约 150,000 行数据.目前,当我尝试运行
I've got a very large MySQL table with about 150,000 rows of data. Currently, when I try and run
SELECT * FROM table WHERE id = '1';
代码运行良好,因为 ID 字段是主索引.但是,对于项目的最新发展,我必须通过另一个字段搜索数据库.例如:
the code runs fine as the ID field is the primary index. However, for a recent development in the project, I have to search the database by another field. For example:
SELECT * FROM table WHERE product_id = '1';
此字段以前未编入索引;但是,我添加了一个,因此 mysql 现在对该字段进行索引,但是当我尝试运行上述查询时,它运行速度非常慢.EXPLAIN 查询显示,当我已经添加了 product_id 字段时,没有索引,因此查询需要 20 分钟到 30 分钟的任何时间来返回单行.
This field was not previously indexed; however, I've added one, so mysql now indexes the field, but when I try to run the above query, it runs very slowly. An EXPLAIN query reveals that there is no index for the product_id field when I've already added one, and as a result the query takes any where from 20 minutes to 30 minutes to return a single row.
我的完整解释结果是:
| id | select_type | table | type | possible_keys| key | key_len | ref | rows | Extra |
+----+-------------+-------+------+--------------+------+---------+------+-------+------------------+
| 1 | SIMPLE | table | ALL | NULL | NULL | NULL | NULL |157211 | Using where |
+----+-------------+-------+------+--------------+------+---------+------+-------+------------------+
注意到我刚刚看了一下,ID 字段存储为 INT 而 PRODUCT_ID 字段存储为 VARCHAR 可能会有所帮助.这可能是问题的根源吗?
It might be helpful to note that I've just taken a look, and ID field is stored as INT whereas the PRODUCT_ID field is stored as VARCHAR. Could this be the source of the problem?
ALTER TABLE `table` ADD INDEX `product_id_index` (`product_id`)
永远不要在 MySQL 中比较 integer 和 strings.如果 id 是 int,请删除引号.
Never compare integer to strings in MySQL. If id is int, remove the quotes.
这篇关于如何向 MySQL 表添加索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何有效地使用窗口函数根据 N 个先前值来决定How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函数根据
在“GROUP BY"中重用选择表达式的结果;条款reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用选择表达式的结果;条款?)
Pyspark DataFrameWriter jdbc 函数的 ignore 选项是忽略整Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函数的 ig
使用 INSERT INTO table ON DUPLICATE KEY 时出错,使用 Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 时出错,使用 for 循环数组
pyspark mysql jdbc load 调用 o23.load 时发生错误 没有合pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 调用 o23.load 时发生错误 没有合适的
如何将 Apache Spark 与 MySQL 集成以将数据库表作为How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何将 Apache Spark 与 MySQL 集成以将数据库表作为