优化查询

发布于 2024-07-25 08:05:50 字数 282 浏览 1 评论 0原文

我的数据库中有一个表,有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最舍不得你 2024-08-01 08:05:50

我不知道我是否在这里遗漏了一些东西,但是有什么问题:

select id,date_time from table where id=?id order by date_time DESC;

并且 ?id 是您正在搜索的 id 的编号...

当然 id 应该是主索引。

I don't know if im missing something here but what's wrong with:

select id,date_time from table where id=?id order by date_time DESC;

and ?id is the number of the id you are searching for...

And of course id should be a primary index.

行至春深 2024-08-01 08:05:50

如果id是唯一的(可能是您的主键),那么您不需要按date_time搜索,并且保证最多只返回一行。

SELECT id, date_time FROM table WHERE id=<value>;

如果id唯一,那么您仍然使用相同的查询,但需要查看索引、其他约束和/或数据库外部的缓存(如果查询变得太慢的。

If id is unique (possibly your primary key), then you don't need to search by date_time and you're guaranteed to only get back at most one row.

SELECT id, date_time FROM table WHERE id=<value>;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文