开发者

How to select records faster from a big database?

开发者 https://www.devze.com 2023-04-08 02:51 出处:网络
I have a mysql table of postcodes with 200000 Records in it. I want to select just one field post_code from it and fetch the postcodes for auto suggestion in a textbox. I will do this using开发者_如何

I have a mysql table of postcodes with 200000 Records in it. I want to select just one field post_code from it and fetch the postcodes for auto suggestion in a textbox. I will do this using开发者_如何学运维 jquery but I want to know which method I should use to create code in php and select records faster?


Without specifics it's hard to give an answer more specific than "Write a faster query".

I'm going to make a huge assumption here and assume you're working on a web application that's populating an autocomplete form.

You don't need all the post-codes! Not even close.

Defer populating the autocomplete until the user starts typing into the postcode field. When that happens, do an AJAX load to get the postcodes from the database.

Why is doing it this way better than just fetching all the post codes?

Because you now know what letter the user's post code starts with.

Knowing the first letter of the post code means you can eliminate any post code in your dataset that doesn't start with the same letter. You'll go from having to fetch 20000 postcodes to having to fetch less than 2000, improving performance by over an order of magnitude. If you wait until the user has entered two letters, you can get the amount of data to be fetched down even further.

Also, I'm assuming you're using an index on your postcode column, right?


I would think you simply want to create a MySql index on "post_code"?

Q: How many postal codes are you planning on fetching at a time (and passing back to the browser)? One? Five? 200,000?


You can use Sphinx-Php Api to deal with big database
But you will need to indexer file for your search for the first time to use this way.
Or can go for Zend Lucene Search


you probably want to filter the records that matches the current input of the user and your query should be as follows:

select postal_code from table_name where postal_code like '<userinput>%' LIMIT 50 eg: '60%'

and send back this result set to your jquery callback function. remember to set a limit on the postal codes retrieved as you dont want to send back too much data which not only hampers the performance but doesnt help the user in a direct way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号