Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL : How to reduce multiple time querying the same table ..Query Optimization
    primarykey
    data
    text
    <p>I have table which contain company list along with location ( location id as foreign key )</p> <p>and user will search for company with in the location in search box and I want return the companies which matches the location ( location id) and if none of the company exists with in that location I want to show the companies which are available at other location( near to asked location - locations are mapped as near in some manner in the different table based on GEO-SPACIAL Coordinates) </p> <p>so My SQL quires for this process is as below </p> <ol> <li>To get Location ID </li> </ol> <blockquote> <p>$loc_id= SELECT id FROM location WHERE location_name=$locationTerm</p> </blockquote> <ol> <li>Use that Location ID to get the companies in the Company Table</li> </ol> <blockquote> <p>$companies= SELECT * FROM companies WHERE location_id= $loc_id</p> </blockquote> <ol> <li><p>Then if the query returned are less than no of rows (say 3) ie $companies.count()&lt;3 then we need to look for nearby areas to raise the companies result. </p></li> <li><p>Get the nearby locations from the Table <strong>NearByLcoations</strong> (Many to Many) consists of near by locations pair </p></li> </ol> <blockquote> <p>$array_loc_ids= SELECT nearby_loc_id FROM NearbyLocations WHERE loc_id= $loc_id;</p> </blockquote> <ol> <li>Then again query the <strong>companies</strong> table for the array of nearby locations ids </li> </ol> <blockquote> <p>$companies= SELECT * FROM companies WHERE in($array_loc_ids);</p> </blockquote> <p>In this process I am hitting the Table Companies multiple times , Location table one time and on condition fail hitting the nearby location table .. Is there any SQL optimization possible to reduce the query time ... This is working for me but I think is very poor coding and method to achieve the result ... Any good suggestion to optimize the query ???</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload