Jump to content

any advice to optomize a search query? runs, but slowly...


slushpuppie

Recommended Posts

(NOTE: i'm changing the names of the tables to make this example a little clearer and hide my project, so if i mis-spelled "items" as "item" in a table name or something like that in my example, that's not an issue)

 

 SELECT DISTINCT `items`.name, `items`.id
FROM `items`
WHERE 1
AND id
IN (
SELECT iid
FROM `item_markings`
WHERE `item_markings`.mid = '9'
AND `item_markings`.amount >=10
)
AND id
IN (
SELECT iid
FROM `item_markings`
WHERE `item_markings`.mid = '73'
AND `item_markings`.amount <=100
)
ORDER BY `items`.name
LIMIT 0 , 30 

 

relevant information about the tables.

table `items` has a unique id field called 'id'.

table `item_markings` has a field called 'iid', which "points" to `items`.id, and 'mid', which "points" to a marking in another table, which right now is irrelevant because i'm passing this 'mid' value into my script.

 

i need to be able to see if an item has a certain marking(s), up to 3 of them.

 

for example:

`items`

"id" | "name"

1 | dog

2 | cat

 

`item_markings`

"iid" | "mid"

1 | 9

1 | 73

2 | 9

3 | 55

 

and say i'm looking for items which have mids of 9 AND 73... i cannot do something like "WHERE mid = 9 OR mid = 73", because that would return 'iid' 2, because it has 9. but i also cannot do "WHERE mid = 9 AND mid = 73" because mid does not equal 9 and 73 at the same time.

 

my initial query at the top of this thread works, however these tables are pretty huge, (2.2 million rows in 'items' and 8.3 million rows in 'item_markings').

 

does anyone know of a different method i could try to try and speed this process up? when i ran it just now i got: "Showing rows 0 - 29 (157 total, Query took 17.8154 sec)" - which really isn't acceptable since i may have hundreds of people running this query at a time.

 

many thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.