bitsCN.com
##---------mysql学习(四)索引的建立--------###
#今天突然开窍了,所以补充点索引方面的知识。
#创建索引,这里仍然以数据较少的mytab表为例:
#原数据为:
mysql> set names gbk;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from mytab;
+----+--------+-----+--------+
| id | name | age | salary |
+----+--------+-----+--------+
| 1 | ?阿琼 | 23 | 1000 |
| 2 | 秋水虾 | 24 | 500 |
| 3 | 害人精 | 22 | 100 |
+----+--------+-----+--------+
3 rows in set (0.00 sec)
#alter table table_name add index index_name (column)==
#create index index_name on table_name(column);
#alter创建索引示例
mysql> alter table mytab add index mytab_name (name);
Query OK, 3 rows affected (0.15 sec)
Records: 3 Duplicates: 0 Warnings: 0
#create创建索引示例:
mysql> create index mytab_id on mytab (id);
Query OK, 3 rows affected (0.16 sec)
Records: 3 Duplicates: 0 Warnings: 0
#查看索引
mysql> show index from mytab;
+-------+------------+----------+--------------+-------------+-----------+------
-------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardi
nality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+------
-------+----------+--------+------+------------+---------+
| mytab | 0 | PRIMARY | 1 | id | A |
3 | NULL | NULL | | BTREE | |
| mytab | 1 | mytab_id | 1 | id | A |
3 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+------
-------+----------+--------+------+------------+---------+
2 rows in set (0.00 sec)
#创建unique索引
mysql> alter table mytab add unique (name);
Query OK, 3 rows affected (0.20 sec)
Records: 3 Duplicates: 0 Warnings: 0
#创建联合索引:
mysql> create index mytab_id_name on mytab (id,name);
Query OK, 3 rows affected (0.20 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> show index from mytab;
+-------+------------+---------------+--------------+-------------+-----------+-
------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation |
Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+---------------+--------------+-------------+-----------+-
------------+----------+--------+------+------------+---------+
| mytab | 0 | PRIMARY | 1 | id | A |
3 | NULL | NULL | | BTREE | |
| mytab | 0 | name | 1 | name | A |
3 | NULL | NULL | | BTREE | |
| mytab | 1 | mytab_name | 1 | name | A |
3 | NULL | NULL | | BTREE | |
| mytab | 1 | mytab_id_name | 1 | id | A |
3 | NULL | NULL | | BTREE | |
| mytab | 1 | mytab_id_name | 2 | name | A |
3 | NULL | NULL | | BTREE | |
+-------+------------+---------------+--------------+-------------+-----------+-
------------+----------+--------+------+------------+---------+
5 rows in set (0.00 sec)
#下面我们尝试一下删除索引,删除用drop
#drop index index_name on table_name==
#alter table table_name drop index index_name;
#drop示例:
mysql> drop index mytab_id on mytab;
Query OK, 3 rows affected (0.17 sec)
Records: 3 Duplicates: 0 Warnings: 0
#alter示例:
mysql> alter table mytab drop index mytab_id_name;
Query OK, 3 rows affected (0.17 sec)
Records: 3 Duplicates: 0 Warnings: 0
#现在发现由于数据数量较小,根本无法判断索引存在的价值。
#
#这里我打算向其中添加3000行数据,这里需要用到Java代码:
#
| 3001 | yiha_2997 | 22 | 5997 |
| 3002 | yiha_2998 | 22 | 5998 |
| 3003 | yiha_2999 | 22 | 5999 |
+------+-----------+-----+--------+
3003 rows in set (0.01 sec)
#######################java代码段##############################
public static void main(String[] args) {
Connection conn=DBConnection.getConnection();
try {
conn.setAutoCommit(false);
PreparedStatement state=conn.prepareStatement
("insert into mytab(name,age,salary) values (?,?,?)");
for(int i=0;i state.setString(1,"yiha_"+i );
state.setInt(2, 22);
state.setInt(3, 3000+i);
state.addBatch();
}
state.executeBatch();
conn.commit();
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
######################数据库连接connection######################
private static String url="jdbc:mysql://" +
"localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8";
private static String driver="com.mysql.jdbc.Driver";
private static String name="root";
private static String pwd="root";
public static Connection getConnection(){
Connection conn;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, name, pwd);
return conn;
###################################################################
##现在数据库中有3003条数据,我们看一下检索数据时间。
#如检索:
id NAME age salary
| 2894 | yiha_2890 | 22 | 5890 |
#id以及name为索引,但是age和salary为非索引
mysql> select * from mytab where id=2894;
+------+-----------+-----+--------+
| id | name | age | salary |
+------+-----------+-----+--------+
| 2894 | yiha_2890 | 22 | 5890 |
+------+-----------+-----+--------+
1 row in set (0.00 sec)
mysql> select * from mytab where salary=5890;
+------+-----------+-----+--------+
| id | name | age | salary |
+------+-----------+-----+--------+
| 2894 | yiha_2890 | 22 | 5890 |
+------+-----------+-----+--------+
1 row in set (0.00 sec)
#可以看出无差别,也许数据仍旧太少,现在将数据提升到30000;
mysql> select * from mytab where id=30000; #id为索引
+-------+------------+-----+--------+
| id | name | age | salary |
+-------+------------+-----+--------+
| 30000 | yiha_29996 | 23 | 32996 |
+-------+------------+-----+--------+
1 row in set (0.00 sec)
mysql> select * from mytab where salary=32996;#salary为非索引
+-------+------------+-----+--------+
| id | name | age | salary |
+-------+------------+-----+--------+
| 30000 | yiha_29996 | 23 | 32996 |
+-------+------------+-----+--------+
1 row in set (0.02 sec)
#由于name也是索引,所以这里试一下用name查找数据:
mysql> select * from mytab where name='yiha_29996';#name为索引
+-------+------------+-----+--------+
| id | name | age | salary |
+-------+------------+-----+--------+
| 30000 | yiha_29996 | 23 | 32996 |
+-------+------------+-----+--------+
1 row in set (0.00 sec)
##虽然在数据多次实验中能够看出索引的作用,但是并不是很明显。以上每一组所耗费时间都是
#个人寻找的出现次数最多的时间。
##个人感觉测试索引效果挺无聊的,索引的作用很多文章都只写了可以精确查找,至于索引如何
#运用貌似很少有相关的东西。数据库中的数据还可以随意扩大,个人感觉先这样吧。

mysqloffersvariousStorageengines,每个suitedfordferentusecases:1)InnodBisidealForapplicationsNeedingingAcidComplianCeanDhighConcurncurnency,supportingtransactionsancions and foreignkeys.2)myisamisbestforread-Heavy-Heavywyworks,lackingtransactionsactionsacupport.3)记忆

MySQL中常见的安全漏洞包括SQL注入、弱密码、权限配置不当和未更新的软件。1.SQL注入可以通过使用预处理语句防止。2.弱密码可以通过强制使用强密码策略避免。3.权限配置不当可以通过定期审查和调整用户权限解决。4.未更新的软件可以通过定期检查和更新MySQL版本来修补。

在MySQL中识别慢查询可以通过启用慢查询日志并设置阈值来实现。1.启用慢查询日志并设置阈值。2.查看和分析慢查询日志文件,使用工具如mysqldumpslow或pt-query-digest进行深入分析。3.优化慢查询可以通过索引优化、查询重写和避免使用SELECT*来实现。

要监控MySQL服务器的健康和性能,应关注系统健康、性能指标和查询执行。1)监控系统健康:使用top、htop或SHOWGLOBALSTATUS命令查看CPU、内存、磁盘I/O和网络活动。2)追踪性能指标:监控查询每秒数、平均查询时间和缓存命中率等关键指标。3)确保查询执行优化:启用慢查询日志,记录并优化执行时间超过设定阈值的查询。

MySQL和MariaDB的主要区别在于性能、功能和许可证:1.MySQL由Oracle开发,MariaDB是其分支。2.MariaDB在高负载环境中性能可能更好。3.MariaDB提供了更多的存储引擎和功能。4.MySQL采用双重许可证,MariaDB完全开源。选择时应考虑现有基础设施、性能需求、功能需求和许可证成本。

MySQL使用的是GPL许可证。1)GPL许可证允许自由使用、修改和分发MySQL,但修改后的分发需遵循GPL。2)商业许可证可避免公开修改,适合需要保密的商业应用。

选择InnoDB而不是MyISAM的情况包括:1)需要事务支持,2)高并发环境,3)需要高数据一致性;反之,选择MyISAM的情况包括:1)主要是读操作,2)不需要事务支持。InnoDB适合需要高数据一致性和事务处理的应用,如电商平台,而MyISAM适合读密集型且无需事务的应用,如博客系统。

在MySQL中,外键的作用是建立表与表之间的关系,确保数据的一致性和完整性。外键通过引用完整性检查和级联操作维护数据的有效性,使用时需注意性能优化和避免常见错误。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

WebStorm Mac版
好用的JavaScript开发工具

Atom编辑器mac版下载
最流行的的开源编辑器

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。