优化查询
我的数据库中有一个表,有 7k 多条记录。 我有一个在该表中搜索特定 id 的查询。(id 是自动递增的)
查询就像这样->
select id,date_time from table order by date_time DESC;
此查询将对 7k + 数据进行所有搜索......无论如何我都无法优化此查询,以便仅对 500 或 1000 条记录进行搜索......因为这些记录将会增加日复一日,我的疑问会变得越来越重。有什么建议吗??????
i have a table in database that is having 7k plus records. I have a query that searches for a particular id in that table.(id is auto-incremented)
query is like this->
select id,date_time from table order by date_time DESC;
this query will do all search on that 7k + data.......isn't there anyways with which i can optimize this query so that search is made only on 500 or 1000 records....as these records will increase day by day and my query will become heavier and heavier.Any suggestions?????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道我是否在这里遗漏了一些东西,但是有什么问题:
并且 ?id 是您正在搜索的 id 的编号...
当然 id 应该是主索引。
I don't know if im missing something here but what's wrong with:
and ?id is the number of the id you are searching for...
And of course id should be a primary index.
如果
id
是唯一的(可能是您的主键),那么您不需要按date_time
搜索,并且保证最多只返回一行。如果
id
不唯一,那么您仍然使用相同的查询,但需要查看索引、其他约束和/或数据库外部的缓存(如果查询变得太慢的。If
id
is unique (possibly your primary key), then you don't need to search bydate_time
and you're guaranteed to only get back at most one row.If
id
is not unique, then you still use the same query but need to look at indexes, other contraints, and/or caching outside the database, if the query becomes too slow.