drqn5418 2010-04-14 09:50
浏览 32
已采纳

检查DB中是否存在记录:单步还是两步?

Suppose you want to get a record from database which returns a large amount of data and requires multiple joins.

So my question would be, is it better to use a single query to check if data exists and get the result if it exists? Or do a more simple query to check if data exists then id record exists, query once again to get the result knowing that it exists.

Example:

3 tables a, b and ab(junction table)

select * from 
from a, b, ab 
where condition
and condition 
and condition 
and condition etc...

or

select id 
from a, b ab 
where condition

then if exists do the query above.

So I don't know if there is any reason to do the second. Any ideas how this affects DB performance or does it matter at all?

  • 写回答

3条回答 默认 最新

  • drasebt1835 2010-04-14 09:56
    关注

    Usually the slowest part in a query is the filter, then it's the indexed search, joins, etc. If it's a lot of data to be transfered - the transfer is also time-consuming. It will be twice slower if you check the existence then extract the data. Just extract the data. If it is there you get it, and if not - you get nothing.

    When extracting from multiple tables, JOIN is faster and more flexible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型