mysql - How to combine these two queries into one optimized query to avoid a Select N+1 -


this online photo library. have 2 tables; "photosearch" , "photos". first, "photosearch", has few columns , contains searchable data photos, such "photoid", "headline", "caption", "people", "datecaptured" , "keywords". has multi-column full-text index on (headline, caption, people, keywords). second table, "photos", contains of photos data; heights, widths, copyrights, caption, id's, dates , more. both have 500k+ rows , headline , caption fields return 2000+ characters.

this approximately how query looks now: (things note: cannot use joins fulltext searching, hence keywords being stored in 1 column - in 'de-normalized' table. also, kind of pseudo code app code elsewhere - it's close )

this particular query being looped through , inside loop, sql statement being executed...

 select photoid photosearch match (headline, caption, people, keywords) against ('"&booleansearchstr&"' in boolean mode) , datecaptured between '"&fromdate&"' , '"&todate&"';" 

this sql statement being executed each row in query above:

 select photoid, setid, eventid, locationid, headline, caption, instructions, datecaptured, dateuploaded, status, uploaderid, thumbh, thumbw, previewh, previeww, + more photos left join events e using (eventid) left join location l using (locationid) photoid = " & photoid & ";" 

when tested, having full-text index on own table, "photosearch", instead of large table, "photos", seemed improve speed somewhat. didn't add "photosearch" table, there - not app. if try joining 2 tables lose second query, lose indexing together, resulting in long times - can't use joins full-text. seemed quickest method. if wasn't full-text , joining problems, have combined both of these queries already.

is possible combine these 2 queries one, make process run more efficiently, rather executing query #2 1000 times each result in query 1?

if join didn't work, try subquery:

select photoid, setid, eventid, locationid, headline, caption, instructions, datecaptured, dateuploaded, status, uploaderid, thumbh, thumbw, previewh, previeww, + more ( select photoid photosearch match (headline, caption, people, keywords) against ('"&booleansearchstr&"' in boolean mode) , datecaptured between '"&fromdate&"' , '"&todate&"';" ) selectedphotos left join photos using (photoid) left join events e using (eventid) left join location l using (locationid) 

or try find out why join didn't work, should have used:

select photoid, setid, eventid, locationid, headline, caption, instructions, datecaptured, dateuploaded, status, uploaderid, thumbh, thumbw, previewh, previeww, + more photosearch left join photos using (photoid) left join events e using (eventid) left join location l using (locationid) match (headline, caption, people, keywords) against ('"&booleansearchstr&"' in boolean mode) , datecaptured between '"&fromdate&"' , '"&todate&"';" 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -