Jump to content

Ajax query is taking about 15 seconds to execute


oracle765

Recommended Posts

SELECT
        COUNT(apl.Location) as Hotel , apl.Location , apl.StateProvince , prl.RegionName , apl.Country, cl.CountryName, ptl.PropertyCategoryDesc
        FROM activepropertylist apl
        INNER JOIN CountryList cl ON apl.Country = cl.CountryCode
        INNER JOIN PropertyTypeList ptl ON apl.PropertyCategory = ptl.PropertyCategory
        INNER JOIN ParentRegionList prl ON apl.RegionID = prl.RegionID
        WHERE
        (apl.Location LIKE '%$q%' OR apl.City LIKE '%$q%' OR cl.CountryName LIKE '%$q%' OR prl.RegionName LIKE '%$q%') GROUP BY apl.Location ORDER BY apl.PropertyCategory LIMIT 25

Hi All

 

just wondering if there is a way to speed this query up with creating indexes or make the query itself perform better, basically it is an autosearch dropdown box for hotel listings in a given place and is running very slow

 

EG Brisbane, which should show Bribane, Australia,AU (6) Hotels, Brisbane, United States of America, USA, (9) Hotels

 

 

 

Link to comment
Share on other sites

You should not have multiple occurences of the word "Brisbane" (or any other city name). "Brisbane" should be stored once in a location table and the id of that record should be in your activepropertylist table. In other words, your data should be normalized. EG

+------------+
| Country    |
+------------+
| countryid  |---+
| countryname|   |
+------------+   |
                 |    +------------+
                 |    | Region     |
                 |    +------------+
                 |    | regionid   |--+
                 |    | regionname |  |
                 +---<| countryid  |  |
                      +------------+  |
                                      |   +---------------+
                                      |   | Location      |
                                      |   +---------------+
                                      |   | locationid    |--+
                                      |   | locationname  |  |
                                      +--<| regionid      |  |
                                          +---------------+  |
                                                             |
                                                             |    +-----------------+
                                                             |    | activeproperty  |
                                                             |    +-----------------+
                                                             |    | propertyid      |
                                                             +---<| locationid      |
                                                                  | propertytype    |
                                                                  | etc.            |
                                                                  +-----------------+

Link to comment
Share on other sites

Just to elaborate on what Barand was saying, whenever you use a LIKE query with a wildcard IN FRONT of the search criteria:

 

LIKE '%$var%'
Then an index can not be used. The entire table has to be scanned.

 

Since you have this:

 

(apl.Location LIKE '%$q%' OR apl.City LIKE '%$q%' OR cl.CountryName LIKE '%$q%' OR prl.RegionName LIKE '%$q%') GROUP BY apl.Loca
Well you are table scanning the location, city, country and region tables at very least.

 

A query that ends with a wild card however, will use an index:

 

LIKE '$var%'
And of course an exact match is best.
Link to comment
Share on other sites

Hi all

 

I am still confused by this, I have just noticed that some of them say East Brisbane, or, Near Brisbane convention center and it does not find these

 

I gather that '$var%' will only find starting with Brisbane.  So my question is what type of index would be best a spatial, full text i am not sure

 

thanks all

Link to comment
Share on other sites

The only type of index that will allow for finding a sub string inside a string with any sort of efficiency, is a full text index. There are also specialized search engines you can use (and most frequently these are what is used to provide searching against a number of facets) -- like sphinx, elastic search or solr.

 

You can apply mysql full text indexes, but there are caveats and a lot of things you have to learn about in order to use them, although the learning curve and deployment issues involved with full text engines are similar.

 

I would suggest you do a bit of research, and certainly you can experiment with adding some mysql full text indexes.

 

If you do go that route, make sure you drop any of the other indexes you've created specifically to speed your search.

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.