Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6870475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:41:18+00:00 2026-05-27T03:41:18+00:00

I have a SQL query that seems to be producing correct results but according

  • 0

I have a SQL query that seems to be producing correct results but according to EXPLAIN isn’t using the spatial index and so is taking much longer than necessary to return all the rows.

SELECT * FROM listings2
WHERE MBRContains(    GeomFromText('POLYGON((32.653132834095 -117.40548330929, 32.653132834095 -117.06151669071, 32.942267165905 -117.06151669071,32.942267165905 -117.40548330929,32.653132834095 -117.40548330929)    )')  ,geoPoint)=true

Interestingly, I figured out that if I remove the =true and let the MBRContains() stand alone, the spatial index gets used properly.

My question is: why is this the case, and can I do something to enable the spatial index to work even when I have =true written at the end of the WHERE clause?

And the only reason I have =true in there at all is because I’m using CodeIgniter’s Active Record and can’t see an easy way around it (so if you know a way around it, that’s another way of solving my problem). (Even switching to just use $this->db->query() would involve a lot of work.)

My table is like this:

CREATE TABLE IF NOT EXISTS `listings2` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `title` varchar(255) NOT NULL DEFAULT '',
      `latitude` decimal(10,6) NOT NULL,
      `longitude` decimal(10,6) NOT NULL,
      `geoPoint` point NOT NULL,
      PRIMARY KEY (`id`),
      KEY `latitude` (`latitude`),
      KEY `longitude` (`longitude`),
      SPATIAL KEY `geoPoint` (`geoPoint`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=404838 ;

Thank you for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:41:18+00:00Added an answer on May 27, 2026 at 3:41 am

    I faced a similar problem while writing a query in hibernate. Here is how I worked around it: Hibernate and MySQL spatial index

    All I did was register a new function with a tiny bit of hack in the MySQLDialect of our application:

    registerFunction("mbr_contains", new SQLFunctionTemplate(Hibernate.BOOLEAN, "MBRContains(?1, ?2) and 1"));
    

    And then I used this function in the HQL query. The query now became something like this:

    ... and mbr_contains(GeomFromText(:${boundaryVariable}), location) = 1 ...
    

    which is valid HQL, and also generates SQL which makes use of the spatial index:

    ... and MBRContains(GeomFromText(?), location) and 1 = 1 ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Sql Query that returns the type of results that I
I have another SQL/access 2007 question that seems really basic but I'm not sure
I have a particular SQL query that seems to suffer from a mysterious performance
I have the following SQL query that runs fairly well, however it seems to
I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have a sql query that runs super fast, around one second, when not
I have a SQL query that takes a very long time to run on
We have a SQL query that pulls a large number of fields from many
I have a SQL query that is supposed to pull out a record and
I have an sql query that has a parameter that can be null in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.