English 中文(简体)
是否在非行有记录:单行或两步?
原标题:Checking if records exist in DB: in single step or two steps?

你们想从数据库中获取一个记录,该数据库接收了大量数据,需要多个链接。

因此,我的问题是,如果存在数据,是否最好使用单一询问来检查数据,如果数据存在,则取得结果? 或者更简单的询问,如果存在数据,则检查数据是否存在,则再次询问是否知道数据存在。

例:

表3

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

select id 
from a, b ab 
where condition

如果存在上述问题的话。

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

最佳回答

通常,电离盘中最缓慢的部分是过滤器,然后是指数化的搜索器,并加入等。 如果要转让大量数据,转让也是费时的。 如果你检查数据是否存在,那么数据就会减少两倍。 仅提取数据。 如果是的话,那么,如果是的话,你就没有。

在从多个表格中抽取时,JOIN更快、更灵活。

问题回答

我通常倾向于“两点查询”办法,而结果必须加以压缩或细化。 例如:

$found_ids = "select id from products where ...complex $condition...."

maybe add to cache ($condition => $found_ids)

$ids_to_show = array_slice($found_ids, $_GET[ page ], $pagesize);

$data = "select * from products, more tables ... where products.id IN($ids_to_show)";

由于第1个查询可以归还一个可能很长的婴儿清单,因此收集所有婴儿数据的时间就会浪费。

如果你担心时间过长,则应考虑另一个说明。 避免在选择发言中使用*。 这可能是一个简单的说明,但直接影响到提问的时间。 更新选定的说明,使用别名、索引库、只选择需要的领域。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签