是否可以使用多個查詢而不是一個查詢?

[英]Is it ever “ok” to use multiple queries instead of one?


In building a web app recently, I started thinking about the information returned from a query I was making:

在最近構建一個web應用程序時,我開始考慮從我正在進行的查詢中返回的信息:

Find the user information and (for simplicity sake) the associated phone numbers tied to this user. Something as simple as:

查找用戶信息和(為了簡單起見)與此用戶關聯的電話號碼。這么簡單的東西:

SELECT a.fname, a.lname, b.phone 
FROM users a 
JOIN users_phones b 
    ON (a.userid = b.userid) 
WHERE a.userid = 12345;

No problem here (yes I'm preventing injection, etc, not the point of this question). When I think about the data that is returned though, I am returning (potentially) several rows of information with that users name on each one. Let's say that single user has 1000 phone numbers associated with it. That's a first name and last name being returned each call a lot. Let's also assume I want to return a lot more than just the first name and last name of that user and in fact I'm starting to return quite a bit of extra rows which I really only needed once.

這里沒有問題(是的,我在防止注射,等等,不是這個問題的重點)。當我考慮返回的數據時,我將返回(潛在的)幾行信息,其中每個用戶都有一個用戶名。假設一個用戶有1000個電話號碼。的名和姓返回每個調用很多。讓我們也假設我想返回一個更多的不僅僅是用戶的名和姓,事實上我開始返回相當多的額外的行,我真的只需要一次。

Are there circumstances in which it is "more appropriate" to make multiple calls to a database?

是否存在對數據庫進行多次調用“更合適”的情況?

e.g.

如。

SELECT firstname, lastname 
FROM users 
WHERE userid = 12345;

SELECT phone 
FROM users_phones 
WHERE userid = 12345;

If the answer is yes, is there a good/proper method of determining when to use multiple queries versus a single one?

如果答案是肯定的,是否有一個好的/合適的方法來決定何時使用多個查詢而不是單個查詢?

3 个解决方案

#1


2  

A query with a JOIN may be slower than two independent queries. It really depends on the type of access you're doing.

連接的查詢可能比兩個獨立查詢慢。這取決於你訪問的類型。

For your example, I'd go with the two query approach. These queries could be executed in parallel, they could be cached, and there's no real reason to JOIN other than for arbitrary presentation concerns.

對於您的示例,我將使用這兩個查詢方法。這些查詢可以並行執行,它們可以被緩存,除了為了任意的表示問題,沒有真正的理由加入。

You'll also want to be concerned about returning duplicate data. In your example it looks like fname and lname would be repeated for each and every phone number, resulting in a lot of data being transmitted that's actually not useful. This is because of the one-to-many relationship you've described.

您還需要關心返回重復的數據。在您的示例中,每個電話號碼都將重復使用fname和lname,導致大量數據被傳輸,這實際上是沒有用的。這是因為您所描述的一對多關系。

Generally you'll want to JOIN if it means sending less data, or because the two queries are not independent.

通常,如果這意味着發送更少的數據,或者因為這兩個查詢不是獨立的,那么您將希望加入。

#2


3  

I think that really depends on your use case. In the example you gave, it seems to make sense to return it as two queries, especially if you're passing that info back to a mobile device where you want to make sure you send them as little data as possible (not everyone has unlimited data.....)

我認為這取決於你的用例。在您給出的示例中,將它作為兩個查詢返回似乎是有意義的,特別是當您將該信息傳遞回移動設備時,您希望確保將它們發送到盡可能少的數據(不是每個人都有無限的數據存儲…)

I'd probably stick a DISTINCT in those queries as well if that's going to make a difference based on your tables.

我可能會在這些查詢中加入一個不同的內容,如果這將對您的表產生影響的話。

#3


0  

This should be driven by the application. Basically, you retrieve in one query all the information needed in one place. If you take this question page as an example, you see your user ID, the reputation counter, and the badge counters. There's no need to retrieve other user profile information when I first display the question page.

這應該由應用程序驅動。基本上,您在一個查詢中檢索所需的所有信息。如果您以這個問題頁面為例,您將看到您的用戶ID、聲譽計數器和徽章計數器。當我第一次顯示問題頁面時,不需要檢索其他用戶配置文件信息。

Only when one clicks on the user ID the rest of the profile may be queried, and may be not even all of it, as there are several tabs on the profile page.

只有當單擊用戶ID時,概要文件的其余部分才可能被查詢,甚至可能不是全部,因為概要文件頁面上有幾個選項卡。

However, if your application is guaranteed to access all 1000 phone numbers at once, along with the user's name, then you probably should fetch them all together.

但是,如果您的應用程序保證同時訪問所有1000個電話號碼,以及用戶的姓名,那么您可能應該將它們一起取回。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/09/13/7206e8b176a9f6e616303e3999c071b3.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com