Experience MySQL's index to improve search performance

Source: Internet
Author: User

1, create a user table with two columns Name,phone

2. Insert 100W Records in Python (any language you like) (LZ's notebook is older, about 1 minutes):

#!/usr/bin/env python#-*-coding:utf-8-*-import mysqldbconn =  mysqldb.connect (host= ' localhost ', user= ' root ', db= ' millionmessage ') cur = conn.cursor () for I in Range (1,1000000):    uname = "user" + str (i)    uphone = "188000" + str (i) C6/>sql = "INSERT into user (Name,phone) VALUES ('%s ', '%s ')"% (Uname,uphone)    cur.execute (SQL) Conn.commit () Cur.close () Conn.close ()

3, search without indexing:

Mysql> SELECT * from user where name= ' user55555 ';
+-------+-----------+-------------+
| UID | name | Phone |
+-------+-----------+-------------+
| 55567 | user55555 | 18800055555 |
+-------+-----------+-------------+
1 row in Set (0.53 sec)

Mysql> Select phone from user where name= ' user55555 ';
+-------------+
| Phone |
+-------------+
| 18800055555 |
+-------------+
1 row in Set (0.46 sec)

4, index The Name property:

Mysql> ALTER TABLE user add index Index_username (name);
Query OK, 0 rows affected (22.27 sec)
records:0 duplicates:0 warnings:0

5, query:

Mysql> SELECT * from user where name= ' user55555 ';
+-------+-----------+-------------+
| UID | name | Phone |
+-------+-----------+-------------+
| 55567 | user55555 | 18800055555 |
+-------+-----------+-------------+
1 row in Set (0.00 sec)


mysql> SELECT * from user where name= ' user999999 ';
+---------+------------+--------------+
| UID | name | Phone |
+---------+------------+--------------+
| 1000011 | user999999 | 188000999999 |
+---------+------------+--------------+
1 row in Set (0.00 sec)


Result seconds out. It can be seen that the index has a great increase in search performance on a database with large amounts of data.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.