Alternative to running multiple DB queries
I’m developing a web app with a search functionality. Generally speaking, the user can search under specific categories or groups of categories. Example:
Mammal (group)
Cat (category)
Dog (category)
Mammal, Cat, and Dog are tables in the DB, and are represented by their own class in the source code. Common fields between Cat and Dog are stored in Mammal; both Cat and Dog have a set of unique fields. I’m trying to figure out the best way to execute a query (or queries) when a user searches under a group (rather than a specific category). For example, the user searches for “all mammals under the age of 4”. As part of the response, I want to return all the fields in the tables belonging to the Mammal category (Cat and Dog, in this case).
Given that the tables Cat and Dog have unique fields, it seems (according to my googling) that I would need to run multiple queries (one for each category). Is this, indeed, the case? If so, what is most efficient way of doing this? And if not, how would I run such a request with a single query?
Essentially, my question is this: What is the most efficient way of executing a query for the situation I’ve described above?
[EDIT]
DB example w/ queries:
https://www.db-fiddle.com/f/na9ctPmi6CjyDB4MNnjycb/3
In the example in the link above, there are two queries which, together, can get all the data for the user's search (described above). I'm wondering if there's a way to do this with a single query, or at least a single call to the DB.
So far, I've tried the naive approach with the multiple query calls. This works fine (insofar as there aren't any errors). My concern is that when the stored data accumulates to the several hundreds/thousands, this approach will become too slow. Furthermore, my current approach requires additional data processing in the source code. For example, if the user wants the top 5 results from the search, then I have to get the top 5 results from each table, create an aggregated collection of the DB results, sort the collection, and pick the top 5 results from the sorted collection to return to the user. I'm wondering if there is a way to accomplish all this on the DB side (assuming that it would be faster).
mysql mybatis
add a comment |
I’m developing a web app with a search functionality. Generally speaking, the user can search under specific categories or groups of categories. Example:
Mammal (group)
Cat (category)
Dog (category)
Mammal, Cat, and Dog are tables in the DB, and are represented by their own class in the source code. Common fields between Cat and Dog are stored in Mammal; both Cat and Dog have a set of unique fields. I’m trying to figure out the best way to execute a query (or queries) when a user searches under a group (rather than a specific category). For example, the user searches for “all mammals under the age of 4”. As part of the response, I want to return all the fields in the tables belonging to the Mammal category (Cat and Dog, in this case).
Given that the tables Cat and Dog have unique fields, it seems (according to my googling) that I would need to run multiple queries (one for each category). Is this, indeed, the case? If so, what is most efficient way of doing this? And if not, how would I run such a request with a single query?
Essentially, my question is this: What is the most efficient way of executing a query for the situation I’ve described above?
[EDIT]
DB example w/ queries:
https://www.db-fiddle.com/f/na9ctPmi6CjyDB4MNnjycb/3
In the example in the link above, there are two queries which, together, can get all the data for the user's search (described above). I'm wondering if there's a way to do this with a single query, or at least a single call to the DB.
So far, I've tried the naive approach with the multiple query calls. This works fine (insofar as there aren't any errors). My concern is that when the stored data accumulates to the several hundreds/thousands, this approach will become too slow. Furthermore, my current approach requires additional data processing in the source code. For example, if the user wants the top 5 results from the search, then I have to get the top 5 results from each table, create an aggregated collection of the DB results, sort the collection, and pick the top 5 results from the sorted collection to return to the user. I'm wondering if there is a way to accomplish all this on the DB side (assuming that it would be faster).
mysql mybatis
ad sqlfiddle that allows to better picture your db structure would be of great help in understanding your problem. That said, what have you tried so far? what errors have you encountered?
– Javier Larroulet
Nov 12 at 14:51
@JavierLarroulet I've update the question, please take a look.
– Aluthren
Nov 12 at 16:56
1
The limiting factor here is that your result sets betweenSELECT * FROM cat
andSELECT * FROM dog
are completely different columns. As such you CAN'T have them in a single result set in any meaningful sense of the term. And... even you did cram them into one (using a UNION) you would still, under the hood, be processing both statements; so it would be a wash.
– JNevill
Nov 12 at 17:00
@JNevill So I'm stuck with the multiple query calls and code-side processing, then?
– Aluthren
Nov 12 at 17:03
1
I believe it's the best approach given your schema (which sounds totally reasonable and appropriate). You could look into using a EAV schema, but I think you will find the added complexity isn't worth it, and it will surely slow some other aspect of your application down because everything has a trade off here. Like your initial search may be faster, but pivoting your results from the EAV schema back into something that your application can deal with may just end up taking longer (and will be more complex).
– JNevill
Nov 12 at 17:06
add a comment |
I’m developing a web app with a search functionality. Generally speaking, the user can search under specific categories or groups of categories. Example:
Mammal (group)
Cat (category)
Dog (category)
Mammal, Cat, and Dog are tables in the DB, and are represented by their own class in the source code. Common fields between Cat and Dog are stored in Mammal; both Cat and Dog have a set of unique fields. I’m trying to figure out the best way to execute a query (or queries) when a user searches under a group (rather than a specific category). For example, the user searches for “all mammals under the age of 4”. As part of the response, I want to return all the fields in the tables belonging to the Mammal category (Cat and Dog, in this case).
Given that the tables Cat and Dog have unique fields, it seems (according to my googling) that I would need to run multiple queries (one for each category). Is this, indeed, the case? If so, what is most efficient way of doing this? And if not, how would I run such a request with a single query?
Essentially, my question is this: What is the most efficient way of executing a query for the situation I’ve described above?
[EDIT]
DB example w/ queries:
https://www.db-fiddle.com/f/na9ctPmi6CjyDB4MNnjycb/3
In the example in the link above, there are two queries which, together, can get all the data for the user's search (described above). I'm wondering if there's a way to do this with a single query, or at least a single call to the DB.
So far, I've tried the naive approach with the multiple query calls. This works fine (insofar as there aren't any errors). My concern is that when the stored data accumulates to the several hundreds/thousands, this approach will become too slow. Furthermore, my current approach requires additional data processing in the source code. For example, if the user wants the top 5 results from the search, then I have to get the top 5 results from each table, create an aggregated collection of the DB results, sort the collection, and pick the top 5 results from the sorted collection to return to the user. I'm wondering if there is a way to accomplish all this on the DB side (assuming that it would be faster).
mysql mybatis
I’m developing a web app with a search functionality. Generally speaking, the user can search under specific categories or groups of categories. Example:
Mammal (group)
Cat (category)
Dog (category)
Mammal, Cat, and Dog are tables in the DB, and are represented by their own class in the source code. Common fields between Cat and Dog are stored in Mammal; both Cat and Dog have a set of unique fields. I’m trying to figure out the best way to execute a query (or queries) when a user searches under a group (rather than a specific category). For example, the user searches for “all mammals under the age of 4”. As part of the response, I want to return all the fields in the tables belonging to the Mammal category (Cat and Dog, in this case).
Given that the tables Cat and Dog have unique fields, it seems (according to my googling) that I would need to run multiple queries (one for each category). Is this, indeed, the case? If so, what is most efficient way of doing this? And if not, how would I run such a request with a single query?
Essentially, my question is this: What is the most efficient way of executing a query for the situation I’ve described above?
[EDIT]
DB example w/ queries:
https://www.db-fiddle.com/f/na9ctPmi6CjyDB4MNnjycb/3
In the example in the link above, there are two queries which, together, can get all the data for the user's search (described above). I'm wondering if there's a way to do this with a single query, or at least a single call to the DB.
So far, I've tried the naive approach with the multiple query calls. This works fine (insofar as there aren't any errors). My concern is that when the stored data accumulates to the several hundreds/thousands, this approach will become too slow. Furthermore, my current approach requires additional data processing in the source code. For example, if the user wants the top 5 results from the search, then I have to get the top 5 results from each table, create an aggregated collection of the DB results, sort the collection, and pick the top 5 results from the sorted collection to return to the user. I'm wondering if there is a way to accomplish all this on the DB side (assuming that it would be faster).
mysql mybatis
mysql mybatis
edited Nov 12 at 16:56
asked Nov 12 at 14:40
Aluthren
536
536
ad sqlfiddle that allows to better picture your db structure would be of great help in understanding your problem. That said, what have you tried so far? what errors have you encountered?
– Javier Larroulet
Nov 12 at 14:51
@JavierLarroulet I've update the question, please take a look.
– Aluthren
Nov 12 at 16:56
1
The limiting factor here is that your result sets betweenSELECT * FROM cat
andSELECT * FROM dog
are completely different columns. As such you CAN'T have them in a single result set in any meaningful sense of the term. And... even you did cram them into one (using a UNION) you would still, under the hood, be processing both statements; so it would be a wash.
– JNevill
Nov 12 at 17:00
@JNevill So I'm stuck with the multiple query calls and code-side processing, then?
– Aluthren
Nov 12 at 17:03
1
I believe it's the best approach given your schema (which sounds totally reasonable and appropriate). You could look into using a EAV schema, but I think you will find the added complexity isn't worth it, and it will surely slow some other aspect of your application down because everything has a trade off here. Like your initial search may be faster, but pivoting your results from the EAV schema back into something that your application can deal with may just end up taking longer (and will be more complex).
– JNevill
Nov 12 at 17:06
add a comment |
ad sqlfiddle that allows to better picture your db structure would be of great help in understanding your problem. That said, what have you tried so far? what errors have you encountered?
– Javier Larroulet
Nov 12 at 14:51
@JavierLarroulet I've update the question, please take a look.
– Aluthren
Nov 12 at 16:56
1
The limiting factor here is that your result sets betweenSELECT * FROM cat
andSELECT * FROM dog
are completely different columns. As such you CAN'T have them in a single result set in any meaningful sense of the term. And... even you did cram them into one (using a UNION) you would still, under the hood, be processing both statements; so it would be a wash.
– JNevill
Nov 12 at 17:00
@JNevill So I'm stuck with the multiple query calls and code-side processing, then?
– Aluthren
Nov 12 at 17:03
1
I believe it's the best approach given your schema (which sounds totally reasonable and appropriate). You could look into using a EAV schema, but I think you will find the added complexity isn't worth it, and it will surely slow some other aspect of your application down because everything has a trade off here. Like your initial search may be faster, but pivoting your results from the EAV schema back into something that your application can deal with may just end up taking longer (and will be more complex).
– JNevill
Nov 12 at 17:06
ad sqlfiddle that allows to better picture your db structure would be of great help in understanding your problem. That said, what have you tried so far? what errors have you encountered?
– Javier Larroulet
Nov 12 at 14:51
ad sqlfiddle that allows to better picture your db structure would be of great help in understanding your problem. That said, what have you tried so far? what errors have you encountered?
– Javier Larroulet
Nov 12 at 14:51
@JavierLarroulet I've update the question, please take a look.
– Aluthren
Nov 12 at 16:56
@JavierLarroulet I've update the question, please take a look.
– Aluthren
Nov 12 at 16:56
1
1
The limiting factor here is that your result sets between
SELECT * FROM cat
and SELECT * FROM dog
are completely different columns. As such you CAN'T have them in a single result set in any meaningful sense of the term. And... even you did cram them into one (using a UNION) you would still, under the hood, be processing both statements; so it would be a wash.– JNevill
Nov 12 at 17:00
The limiting factor here is that your result sets between
SELECT * FROM cat
and SELECT * FROM dog
are completely different columns. As such you CAN'T have them in a single result set in any meaningful sense of the term. And... even you did cram them into one (using a UNION) you would still, under the hood, be processing both statements; so it would be a wash.– JNevill
Nov 12 at 17:00
@JNevill So I'm stuck with the multiple query calls and code-side processing, then?
– Aluthren
Nov 12 at 17:03
@JNevill So I'm stuck with the multiple query calls and code-side processing, then?
– Aluthren
Nov 12 at 17:03
1
1
I believe it's the best approach given your schema (which sounds totally reasonable and appropriate). You could look into using a EAV schema, but I think you will find the added complexity isn't worth it, and it will surely slow some other aspect of your application down because everything has a trade off here. Like your initial search may be faster, but pivoting your results from the EAV schema back into something that your application can deal with may just end up taking longer (and will be more complex).
– JNevill
Nov 12 at 17:06
I believe it's the best approach given your schema (which sounds totally reasonable and appropriate). You could look into using a EAV schema, but I think you will find the added complexity isn't worth it, and it will surely slow some other aspect of your application down because everything has a trade off here. Like your initial search may be faster, but pivoting your results from the EAV schema back into something that your application can deal with may just end up taking longer (and will be more complex).
– JNevill
Nov 12 at 17:06
add a comment |
1 Answer
1
active
oldest
votes
From your sqlfiddle:
select * from cat left join mammal on (cat.id = mammal.id) where age < 4
union all
select * from dog left join mammal on (dog.id = mammal.id) where age < 4
A few points:
You only want
left join
if, for example, you'd want to show lines for mammals that don't have correspondingdog
s orcat
s. In your case, it doesn't seem like that's what you want, so it would be better to use aninner join
(or simplyjoin
, which defaults to an inner one). That way, if there are no dogs, and only 2 cats, the results would only show 2 lines, instead of 2 cats + 1 non-existing dog.Making one table per mammal type won't scale. What happens if your users now want to add coyotes? Or rabbits? Every single time a new mammal needs to be added to the system, you need to create a new table. The proper way to do this would be to create a join table that would detail:
A table
animal_type
, with lines something like this:animal_type_id name
Then you'd have a separate table called
attribute_type
with something like this:attribute_id name
A separate table called
animals
:animal_id animal_type_id
And then finally you'd have a separate table called
animal_attributes
:id animal_id attribute_id value
Now, you can simply add animals by inserting a row in animals
, specifying the animal_type_id
, which could be cat/dog/whatever. You simply need to have created the animal_type beforehand. And then, you add attributes to your animal by creating rows in animal_attributes
, referencing the animal_id
you just created, as well as the proper animal_attribute_id
, which could be shared attributes like color, length, size, and unique cat/dog/animal fields.
Thank you for pointing out the "join" clause details; your assumption is correct, and I'll make the appropriate changes. As for the DB schema suggestions, the available categories are predetermined and not under the control of the user. While it is possible that new categories could be added by the devs later on, it is very unlikely to happen. While the structure has it's merits, it doesn't seem like it would accurately reflect the relationships between categories for the purposes of this application. I'm going to give it some more thought, and get back to you on it, though
– Aluthren
Nov 12 at 17:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264478%2falternative-to-running-multiple-db-queries%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From your sqlfiddle:
select * from cat left join mammal on (cat.id = mammal.id) where age < 4
union all
select * from dog left join mammal on (dog.id = mammal.id) where age < 4
A few points:
You only want
left join
if, for example, you'd want to show lines for mammals that don't have correspondingdog
s orcat
s. In your case, it doesn't seem like that's what you want, so it would be better to use aninner join
(or simplyjoin
, which defaults to an inner one). That way, if there are no dogs, and only 2 cats, the results would only show 2 lines, instead of 2 cats + 1 non-existing dog.Making one table per mammal type won't scale. What happens if your users now want to add coyotes? Or rabbits? Every single time a new mammal needs to be added to the system, you need to create a new table. The proper way to do this would be to create a join table that would detail:
A table
animal_type
, with lines something like this:animal_type_id name
Then you'd have a separate table called
attribute_type
with something like this:attribute_id name
A separate table called
animals
:animal_id animal_type_id
And then finally you'd have a separate table called
animal_attributes
:id animal_id attribute_id value
Now, you can simply add animals by inserting a row in animals
, specifying the animal_type_id
, which could be cat/dog/whatever. You simply need to have created the animal_type beforehand. And then, you add attributes to your animal by creating rows in animal_attributes
, referencing the animal_id
you just created, as well as the proper animal_attribute_id
, which could be shared attributes like color, length, size, and unique cat/dog/animal fields.
Thank you for pointing out the "join" clause details; your assumption is correct, and I'll make the appropriate changes. As for the DB schema suggestions, the available categories are predetermined and not under the control of the user. While it is possible that new categories could be added by the devs later on, it is very unlikely to happen. While the structure has it's merits, it doesn't seem like it would accurately reflect the relationships between categories for the purposes of this application. I'm going to give it some more thought, and get back to you on it, though
– Aluthren
Nov 12 at 17:35
add a comment |
From your sqlfiddle:
select * from cat left join mammal on (cat.id = mammal.id) where age < 4
union all
select * from dog left join mammal on (dog.id = mammal.id) where age < 4
A few points:
You only want
left join
if, for example, you'd want to show lines for mammals that don't have correspondingdog
s orcat
s. In your case, it doesn't seem like that's what you want, so it would be better to use aninner join
(or simplyjoin
, which defaults to an inner one). That way, if there are no dogs, and only 2 cats, the results would only show 2 lines, instead of 2 cats + 1 non-existing dog.Making one table per mammal type won't scale. What happens if your users now want to add coyotes? Or rabbits? Every single time a new mammal needs to be added to the system, you need to create a new table. The proper way to do this would be to create a join table that would detail:
A table
animal_type
, with lines something like this:animal_type_id name
Then you'd have a separate table called
attribute_type
with something like this:attribute_id name
A separate table called
animals
:animal_id animal_type_id
And then finally you'd have a separate table called
animal_attributes
:id animal_id attribute_id value
Now, you can simply add animals by inserting a row in animals
, specifying the animal_type_id
, which could be cat/dog/whatever. You simply need to have created the animal_type beforehand. And then, you add attributes to your animal by creating rows in animal_attributes
, referencing the animal_id
you just created, as well as the proper animal_attribute_id
, which could be shared attributes like color, length, size, and unique cat/dog/animal fields.
Thank you for pointing out the "join" clause details; your assumption is correct, and I'll make the appropriate changes. As for the DB schema suggestions, the available categories are predetermined and not under the control of the user. While it is possible that new categories could be added by the devs later on, it is very unlikely to happen. While the structure has it's merits, it doesn't seem like it would accurately reflect the relationships between categories for the purposes of this application. I'm going to give it some more thought, and get back to you on it, though
– Aluthren
Nov 12 at 17:35
add a comment |
From your sqlfiddle:
select * from cat left join mammal on (cat.id = mammal.id) where age < 4
union all
select * from dog left join mammal on (dog.id = mammal.id) where age < 4
A few points:
You only want
left join
if, for example, you'd want to show lines for mammals that don't have correspondingdog
s orcat
s. In your case, it doesn't seem like that's what you want, so it would be better to use aninner join
(or simplyjoin
, which defaults to an inner one). That way, if there are no dogs, and only 2 cats, the results would only show 2 lines, instead of 2 cats + 1 non-existing dog.Making one table per mammal type won't scale. What happens if your users now want to add coyotes? Or rabbits? Every single time a new mammal needs to be added to the system, you need to create a new table. The proper way to do this would be to create a join table that would detail:
A table
animal_type
, with lines something like this:animal_type_id name
Then you'd have a separate table called
attribute_type
with something like this:attribute_id name
A separate table called
animals
:animal_id animal_type_id
And then finally you'd have a separate table called
animal_attributes
:id animal_id attribute_id value
Now, you can simply add animals by inserting a row in animals
, specifying the animal_type_id
, which could be cat/dog/whatever. You simply need to have created the animal_type beforehand. And then, you add attributes to your animal by creating rows in animal_attributes
, referencing the animal_id
you just created, as well as the proper animal_attribute_id
, which could be shared attributes like color, length, size, and unique cat/dog/animal fields.
From your sqlfiddle:
select * from cat left join mammal on (cat.id = mammal.id) where age < 4
union all
select * from dog left join mammal on (dog.id = mammal.id) where age < 4
A few points:
You only want
left join
if, for example, you'd want to show lines for mammals that don't have correspondingdog
s orcat
s. In your case, it doesn't seem like that's what you want, so it would be better to use aninner join
(or simplyjoin
, which defaults to an inner one). That way, if there are no dogs, and only 2 cats, the results would only show 2 lines, instead of 2 cats + 1 non-existing dog.Making one table per mammal type won't scale. What happens if your users now want to add coyotes? Or rabbits? Every single time a new mammal needs to be added to the system, you need to create a new table. The proper way to do this would be to create a join table that would detail:
A table
animal_type
, with lines something like this:animal_type_id name
Then you'd have a separate table called
attribute_type
with something like this:attribute_id name
A separate table called
animals
:animal_id animal_type_id
And then finally you'd have a separate table called
animal_attributes
:id animal_id attribute_id value
Now, you can simply add animals by inserting a row in animals
, specifying the animal_type_id
, which could be cat/dog/whatever. You simply need to have created the animal_type beforehand. And then, you add attributes to your animal by creating rows in animal_attributes
, referencing the animal_id
you just created, as well as the proper animal_attribute_id
, which could be shared attributes like color, length, size, and unique cat/dog/animal fields.
answered Nov 12 at 17:13
mjuarez
9,68973751
9,68973751
Thank you for pointing out the "join" clause details; your assumption is correct, and I'll make the appropriate changes. As for the DB schema suggestions, the available categories are predetermined and not under the control of the user. While it is possible that new categories could be added by the devs later on, it is very unlikely to happen. While the structure has it's merits, it doesn't seem like it would accurately reflect the relationships between categories for the purposes of this application. I'm going to give it some more thought, and get back to you on it, though
– Aluthren
Nov 12 at 17:35
add a comment |
Thank you for pointing out the "join" clause details; your assumption is correct, and I'll make the appropriate changes. As for the DB schema suggestions, the available categories are predetermined and not under the control of the user. While it is possible that new categories could be added by the devs later on, it is very unlikely to happen. While the structure has it's merits, it doesn't seem like it would accurately reflect the relationships between categories for the purposes of this application. I'm going to give it some more thought, and get back to you on it, though
– Aluthren
Nov 12 at 17:35
Thank you for pointing out the "join" clause details; your assumption is correct, and I'll make the appropriate changes. As for the DB schema suggestions, the available categories are predetermined and not under the control of the user. While it is possible that new categories could be added by the devs later on, it is very unlikely to happen. While the structure has it's merits, it doesn't seem like it would accurately reflect the relationships between categories for the purposes of this application. I'm going to give it some more thought, and get back to you on it, though
– Aluthren
Nov 12 at 17:35
Thank you for pointing out the "join" clause details; your assumption is correct, and I'll make the appropriate changes. As for the DB schema suggestions, the available categories are predetermined and not under the control of the user. While it is possible that new categories could be added by the devs later on, it is very unlikely to happen. While the structure has it's merits, it doesn't seem like it would accurately reflect the relationships between categories for the purposes of this application. I'm going to give it some more thought, and get back to you on it, though
– Aluthren
Nov 12 at 17:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264478%2falternative-to-running-multiple-db-queries%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
ad sqlfiddle that allows to better picture your db structure would be of great help in understanding your problem. That said, what have you tried so far? what errors have you encountered?
– Javier Larroulet
Nov 12 at 14:51
@JavierLarroulet I've update the question, please take a look.
– Aluthren
Nov 12 at 16:56
1
The limiting factor here is that your result sets between
SELECT * FROM cat
andSELECT * FROM dog
are completely different columns. As such you CAN'T have them in a single result set in any meaningful sense of the term. And... even you did cram them into one (using a UNION) you would still, under the hood, be processing both statements; so it would be a wash.– JNevill
Nov 12 at 17:00
@JNevill So I'm stuck with the multiple query calls and code-side processing, then?
– Aluthren
Nov 12 at 17:03
1
I believe it's the best approach given your schema (which sounds totally reasonable and appropriate). You could look into using a EAV schema, but I think you will find the added complexity isn't worth it, and it will surely slow some other aspect of your application down because everything has a trade off here. Like your initial search may be faster, but pivoting your results from the EAV schema back into something that your application can deal with may just end up taking longer (and will be more complex).
– JNevill
Nov 12 at 17:06