Like many PHP developers, I used a relatively simple data structure when I first set up a dynamic website. PHP is indeed very convenient in connecting to the database, some people think that PHP does not have a unified interface when connecting to different databases... like many PHP developers, I used a relatively simple data structure when I first set up a dynamic website. PHP is indeed very convenient in connecting to the database, some people think that PHP does not have a unified interface when connecting to different databases, which is inconvenient. In fact, this can be achieved through some extension libraries, you can create and use databases without reading a lot of design documents, which is one of the main reasons for PHP's success.
Some time ago, a very senior programmer asked me what index is, and I was very surprised. I think it will not be a huge drop, because there are thousands of developers, most of them may use MySQL and have not received any formal training on databases. although they have done some development for customers, they do not know much about how to create appropriate indexes for databases, so I started to write a related article.
The most common case is to create an index for the field that appears in the where clause. for convenience, we should first create the following table.
CREATE TABLE mytable ( id serial primary key, category_id int not null default 0, user_id int not null default 0, adddate int not null default 0 );
It is easy, but it is enough to explain this problem. if you use statements similar to the following in queries:
SELECT * FROM mytable WHERE category_id = 1;
The most direct response is to create a simple index for category_id:
CREATE INDEX mytable_categoryidON mytable (category_id);
OK, OK? Don't be excited. what if you have more than one choice? For example:
SELECT * FROM mytable WHERE category_id = 1 AND user_id = 2;
Your first reaction may be to create an index for user_id. this is not the best method. you can create multiple indexes.
Create index mytable_categoryid_userid ON mytable (category_id, user_id );
Have you noticed my habits in naming? I am using "table name_field 1 name_field 2", and you will soon know why I did this.
Now you have created an index for an appropriate field. However, it is a bit difficult. you may ask, will the database actually use these indexes? The test is OK. For most databases, this is very easy. you only need to use the EXPLAIN command:
Explain select * FROM mytable WHERE category_id = 1 AND user_id = 2; This is what has s 7.1 returns (exactly as I expected) NOTICE: QUERY PLAN: index Scan using mytable_categoryid_userid on mytable (cost = 0. 00 .. 2.02 rows = 1 width = 16) // open source code phprm.com EXPLAIN
The above is the data of IPVs. we can see that the database uses an index during query, which is a good start and uses the second index I created, see the benefits of my naming above. you will immediately know that it uses the appropriate index.
Next, let's make it a little more complex. what if there is an order by clause? Believe it or not, most databases will benefit from the index when using order.
SELECT * FROM mytableWHERE category_id=1 AND user_id=2
Article link:
Save this article for favorites!