对于非常简单的选择,MySQL 性能不佳
我的 mysql 查询的性能有问题。我有一个非常大的表
create table query(
id Integer,
session Integer,
time Integer,
name Integer,
region Integer);
数据量 - 2 GB。我在“名称”上创建了索引 - 7 GB。
我的查询如下所示:
select count(id) from query where name=somevalue;
我不会添加任何新数据,并且我使用标准“my-huge.cnf”。尽管如此,我每次查询都会花费大约 4-5 秒,我将执行大约 9-10*45000 次查询。如果我的计算机有 2GB 内存,我应该更改哪些选项来提高速度。
I've got a problem with perfomance of my mysql queries. I've got a very big table
create table query(
id Integer,
session Integer,
time Integer,
name Integer,
region Integer);
Volume of data - 2 gb .I've made index on "name" - 7 gb.
My queries look like:
select count(id) from query where name=somevalue;
I wouldn't add any new data, and I used standard "my-huge.cnf". Still, I spend about 4-5 seconds per query, I'm going to do about 9-10*45000 queries. Which options should I change to increase speed, if my computer has 2gb memory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
count(*) 的运行速度可能会稍快一些。
您也可以考虑将计数缓存在单独的表中并从中查询。
A count(*) may run marginally faster.
Also you may consider caching the counts in a separate table and querying from that.
如果您从不更改数据,那么您应该考虑对所有可能的名称运行一次查询 (
SELECT DISTINCT(name) FROM query
),然后存储COUNT()< 的值/code> 在缓存中。为此,您可以创建一个表
cache
,其中name
和total
作为列,并用运行SELECT 的结果填充它name, COUNT(*) AS 总计 FROM 查询 WHERE name = 'name'
每个名称。然后,您只需
从缓存中选择总计 WHERE name = 'name'
,这将非常快。If you're never changing the data, then you should consider running the queries for all possible names (
SELECT DISTINCT(name) FROM query
) once, then storing the value ofCOUNT()
in a cache. For that purpose, you could create a tablecache
withname
andtotal
as columns, and populate it with the results of runningSELECT name, COUNT(*) AS total FROM query WHERE name = 'name'
for each name.You will then simply
SELECT total FROM cache WHERE name = 'name'
, which will be very fast.