Jump to content

MySQL, Indexing and Optimization


bachx

Recommended Posts

Hello, I've noticed my scripts are becoming more and more slower as more users are coming to my site. I've pointed the problem to my queries, but I some help in order to optimize them.

 

All my tables are indexed (By an auto-incremented primary key called 'ID'), but it seems that I wasn't using those indexes at all. For example, most of my SELECT statements are like this:

SELECT * FROM users WHERE name='xxx'

 

Does the above query scan through every row, or will it return the result directly? If not, I seriously need to change that. So, will the below query resolve my problem? (By selecting the data using the ID not the Name)

 

SELECT * FROM users WHERE ID='xxx'

 

Also, does the same rule applies to the UPDATE query statements, or just the SELECT ones? And what other optimizations/tips you recommend in general for my PHP/MySQL scripts? Thanks.

 

 

Link to comment
Share on other sites

Yes the INDEX will speed up any statement that has a WHERE clause searching the name.

 

It might be better practice to instead use WHERE ID = 'xxx', if your index is using to much space I believe MySQL can create partial indexes from a shell command as well.

 

Avoid using SELECT * FROM table.

 

* means your retrieving all the data from the table where the conditions are met. Instead SELECT field1,field2 FROM table would consume less memory and time then retrieving unecessary fields you don't need.

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.