[英]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:


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.


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




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 个解决方案



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.


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




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.




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.


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.


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.




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