切身体验MySQL的索引对搜索性能的提升

WBOY
풀어 주다: 2016-06-07 16:25:41
원래의
938명이 탐색했습니다.

亲身体验MySQL的索引对搜索性能的提升 1,创建一个user表,包含两列name,phone 2,用python(你喜欢的任何语言)插入100W条记录(lz的笔记本比较老,大概用了1分钟吧): #!/usr/bin/env python# -*- coding:utf-8 -*-import MySQLdbconn = MySQLdb.connect(hos

亲身体验MySQL的索引对搜索性能的提升

1,创建一个user表,包含两列name,phone

2,用python(你喜欢的任何语言)插入100W条记录(lz的笔记本比较老,大概用了1分钟吧):

<span>#!/usr/bin/env python
# -*- coding:utf-8 -*-

import MySQLdb

conn =  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)
    sql = "insert into user(name,phone) values('%s','%s')" % (uname,uphone)
    cur.execute(sql)

conn.commit()
cur.close()
conn.close()
</span>
로그인 후 복사

3,在没建立索引的情况下搜索:

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,对name属性建立索引:

mysql> alter table user add index index_username(name);
Query OK, 0 rows affected (22.27 sec)
Records: 0  Duplicates: 0  Warnings: 0

5, 查询:

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)


结果秒出。可见在海量数据的数据库上,索引对搜索性能的提升是非常大的。

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!