Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

EbrahemSamer's avatar

How to speed up search ( 1 million record ) PHP & MYSQL?

I am searching on table using query like this

select * from table_name where f_name = .... and l_name = ..... and gender = male and ... ............. etc

the result shows in 3 seconds, is there is any thing i can do to make it fast.. ( I am using PDO ).

0 likes
4 replies
ismaile's avatar
ismaile
Best Answer
Level 30

You should definitely look at indexes. If you perform some queries frequently, indexing the related fields would immediately reduce the response time.

You can think of indexes like ordering according to one or more fields. If you look for a word in an unordered dictionary, you would have to check for each page until you find the word. At best, the result is on the first page, at worst, it is in the last page you checked and it took a lot of time. Indexing puts some order so you can open it at the middle then jump to the first half or second half and so on.

By default, the id is usually the primary key and is indexed. However, you can index several keys.

https://laravel.com/docs/6.x/migrations#indexes

jlrdw's avatar

Yes indexes, but also use as few fields as possible.

For example, at the Social Security Office, they don't

name like ...  age = ...  phone = ....

They can quickly find you by entering one thing, your SSN.

Also some older data can sometimes be archived. At a non profit I did a bookkeeping app for, only the current years expense and income is needed, not 5 years ago. But it is backed up (archived) and saved in case.

ahmeddabak's avatar

Also be careful not to over use indexes, it may improve the search speed, but over indexing will slow down inserting and updating records in you table

Please or to participate in this conversation.