mysql 性能与情况

标签 mysql performance

如果我有一个包含 10k 行的表 TableA,并且我想搜索 id > 8000 的所有行

当我使用SQL语句SELECT * FROM TableA WHERE id > 8000来搜索它们时,MySQL会做什么?它会搜索 10k 行并返回符合条件的 2k 行,还是忽略这 8k 行并返回 2k 行数据?

我还需要每天在数据库中存储大量数据,并且需要快速搜索今天记录。一张大 table 仍然是最好的方法还是有其他可用的解决方案?

或者最好创建 2 个表。 1 表示所有记录,1 表示今天的记录,当新数据到来时,两个表都会插入,但第二天第二个表的记录将被删除。

在比较 select 速度时哪种方法更好,或者任何其他好的方法可以用于这种情况?

Actually i don't have the real database here now but i just worry about which way/method can be better in that case

Updated information below at (8-12-2016 11:00)

I am using InnoDB but i will use the date as the search key and it is not a PK.

Returning 2k rows is just a extremely case for study but in the real case may returning (User Numbers * each record for that User), so if i got 100 user and they make 10 record in that day, i may need to returning 1k rows record.

My real case is i need to store all user records per days (maybe 10 records per 1 user) and i need to generate the rank for the last day records and the last 7 days records so i just worry if i just search the last day records in a large table, would it be slow or create another table just for save the last day records?

最佳答案

  • 您获取的数据是否超过表的 20%? (20% 这个数字并不准确。)
  • 主键是否位于id上?或者它是辅助 key ?
  • 您使用的是 ENGINE=InnoDB 吗?

案例:InnoDB 和 PRIMARY KEY(id):执行将从 8000 开始,直到完成。这是最佳的

案例:InnoDB,id是辅助键,并且正在获取表的“小”百分比:将使用索引;它是一个 BTree,从 8000 扫描到末尾,跳转到数据(通过 PK)来查找行。

案例:InnoDB,id是次要的,且百分比大:索引将被忽略,并且将扫描整个表(“表扫描”),忽略不匹配的行WHERE 子句。由于所有“跳转到数据”,表扫描可能比前一种情况更快。

其他评论:

  • 10K 行对于表格来说“很小”。
  • 就结果集而言,返回 2K 行是“大”的。你在用它们做什么?
  • 是否有进一步的过滤可以移交给 MySQL,这样您就不会收回所有 2K 数据?考虑将 COUNTSUMGROUP BYFULLTEXT 搜索索引等结合使用。

More tips关于索引。

关于mysql 性能与情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41011520/

相关文章:

mysql - Spring Cloud数据流无法连接到MySQL DB

php - 无法让变量在 WPDB UPDATE 查询中工作

C++ 多次写入文件的有效方法

javascript - 自己使用几个 for 循环还是将逻辑委托(delegate)给 promise 性能更好?

php - 删除用户最佳实践?

javascript - sails js 错误 : Handshake inactivity timeout

php - 在 SQL While 循环中找不到行时回显某些内容

performance - 与 Apache 相比,Node.js 的性能如何?

javascript - 在 ASP.NET 网站上将 javascript 放在结束正文标记之前可以吗?

sql-server - 使用数据库引擎优化顾问建立索引