0

向表中添加主键(自动增量)以获得更好的性能是否有用?此 ID 不会在查询中使用,但无论如何它可能对性能很有用。

我的一张表没有主键/唯一/索引,工作速度非常慢。那么添加这样的 ID 更好还是应该为一个主键创建 3 列(因为它们必须在一起才能唯一)?

4

3 回答 3

1

有额外的很好PK,您可以UNIQUE在其他列上添加索引以支持它们

于 2013-07-24T10:09:55.210 回答
1

我喜欢总是有一个 ID 整数自动增量,因为您永远不知道何时需要从其他人那里引用此表或根据属性查找特定行(例如 findById()。ID 整数自动增量列非常适合它哈哈 )

话虽如此,查询速度慢的问题取决于每个查询。看看你的 WHERE 子句。您是否通过这 3 列一起搜索是唯一的?然后在这 3 列之上添加一个索引(并使其唯一,因为它们一起是唯一的)。如果您的查询仅按一列搜索,请在其上添加索引。

于 2013-07-24T10:16:59.367 回答
0

Indexes only improve performance of queries that search the column being indexed. A primary key that isn't used will have no effect on performance.

If your queries are running slowly, make sure you have indexes on the columns that you're searching heavily (a column doesn't have to be unique to have an index). And use EXPLAIN to see how MySQL is optimizing the query, and whether it's making use of those indexes.

If you're having trouble with a particular query, start a new question where you post your database schema and the query in question, and we may be able to help you.

于 2013-07-24T10:14:37.927 回答