mysql结构与sql执行流程

Mysql的大体结构
客户端:用于链接mysql的软件

连接池:

sql接口:

查询解析器:

MySQL连接层
连接层:

应用程序通过接口(如odbc,jdbc)来连接mysql,最先连接处理的是连接层。

连接层包括:

1.通信协议

2.线程处理

3.用户名密码认证

show variables like "%max_connections%";查询最大连接数

短连接 mysql数据库的连接 => 优化器解析 => 数据返回 => 关闭连接

长链接 mysql数据库的连接 => 优化器解析 => 数据返回 => 连接 => 数据返回 ... 8个小时

连接权限校验:

1.用户名密码

2.连接权限校验

select 'host','user' from mysql.'user'; create user 'admin'@'192.168.15.%' identified by 'root';

-- create user '用户名'@'ip' IDENTIFIED BY "密码" grant all on . to 'admin'@'192.168.15.%' WITH GRANT OPTION;

-- 设置允许远程用户访问

mysql的sql层
SQL层:

sql层是mysql的核心,mysql的核心服务都是在这层实现的。主要包含权限判断、解析器、预处理、查询优化器、缓存和执行计划

mysql8没有查询缓存

.权限判断可以审核用户有没有访问某个库、某个表,或者表里某行数据的权限。

.查询解析器针对SQL语句进行解析,判断语法是否正确。

.预处理器对解析器无法解析的语法进行处理。

.查询优化器对SQL进行改写和相应的优化,并生成最优的执行计划,就可以调用程序的API接口,通过存储引擎层访问数据。

sql接口 => 接收sql语句

sql语句的类型:

dml(insert,update,delete)

query(select)

ddl(alter)

status(show status)

create user 'xinkong'@'%' identified by "root";

Grant all on xx.* to 'xinkong'@'%' with grant option;

alter table user add index idx_age(age); --添加索引

alter table user add index idx_name_age(name,age);

drop index idx_age on user; ---删除索引

解析器:

select * from user where name = 'xx' and age = 18;

优化器:

SQL语句在 查询之前会使用查询优化器对查询进行优化,同时验证用户是否有权限进行查询。

获取表结构信息:字段信息,字段类型,表存储位置 ,索引信息

权限校验:Grant all on xx.* to '用户名'@'host' with grant option;

条件过滤与调整,根据索引确定计划:

set optimizer_trace="enabled=on";--开启trace查看优化器的结果

set end_marker_in_json=on;--增加注释

select * from information_schema.optimizer_trace \G;--查询打印执行计划

{"steps": [{"join_preparation": {"select#": 1,"steps": [{"expanded_query": "/* select#1 */ select `user`.`id` AS `id`,`user`.`name` AS `name` from `user` where ((`user`.`id` > 100000) and (`user`.`age` > 30))"}] /* steps */} /* join_preparation */},{"join_optimization": {"select#": 1,"steps": [{"condition_processing": {"condition": "WHERE","original_condition": "((`user`.`id` > 100000) and (`user`.`age` > 30))","steps": [{"transformation": "equality_propagation","resulting_condition": "((`user`.`id` > 100000) and (`user`.`age` > 30))"},{"transformation": "constant_propagation","resulting_condition": "((`user`.`id` > 100000) and (`user`.`age` > 30))"},{"transformation": "trivial_condition_removal","resulting_condition": "((`user`.`id` > 100000) and (`user`.`age` > 30))"}] /* steps */} /* condition_processing */},{"substitute_generated_columns": {} /* substitute_generated_columns */},{"table_dependencies": [{"table": "`user`","row_may_be_null": false,"map_bit": 0,"depends_on_map_bits": [] /* depends_on_map_bits */}] /* table_dependencies */},{"ref_optimizer_key_uses": [] /* ref_optimizer_key_uses */},{"rows_estimation": [{"table": "`user`","range_analysis": {"table_scan": {"rows": 97925,"cost": 9866.85} /* table_scan */,"potential_range_indexes": [{"index": "PRIMARY","usable": true,"key_parts": ["id"] /* key_parts */},{"index": "idx_age","usable": true,"key_parts": ["age","id"] /* key_parts */},{"index": "idx_name_age","usable": true,"key_parts": ["name","age","id"] /* key_parts */}] /* potential_range_indexes */,"best_covering_index_scan": {"index": "idx_name_age","cost": 10064.8,"chosen": false,"cause": "cost"} /* best_covering_index_scan */,"setup_range_conditions": [] /* setup_range_conditions */,"group_index_range": {"chosen": false,"cause": "not_group_by_or_distinct"} /* group_index_range */,"skip_scan_range": {"potential_skip_scan_indexes": [{"index": "PRIMARY","usable": false,"cause": "query_references_nonkey_column"},{"index": "idx_age","usable": false,"cause": "query_references_nonkey_column"},{"index": "idx_name_age","tree_travel_cost": 0.85,"num_groups": 49161,"rows": 97925,"cost": 98534.7}] /* potential_skip_scan_indexes */} /* skip_scan_range */,"best_skip_scan_summary": {"type": "skip_scan","index": "idx_name_age","key_parts_used_for_access": ["name","age"] /* key_parts_used_for_access */,"range": ["30 < age"] /* range */,"chosen": false,"cause": "cost"} /* best_skip_scan_summary */,"analyzing_range_alternatives": {"range_scan_alternatives": [{"index": "PRIMARY","ranges": ["100000 < id"] /* ranges */,"index_dives_for_eq_ranges": true,"rowid_ordered": true,"using_mrr": false,"index_only": false,"rows": 48962,"cost": 4911.7,"chosen": true},{"index": "idx_age","ranges": ["30 < age"] /* ranges */,"index_dives_for_eq_ranges": true,"rowid_ordered": false,"using_mrr": false,"index_only": false,"rows": 48962,"cost": 17137,"chosen": false,"cause": "cost"},{"index": "idx_name_age","chosen": false,"cause": "no_valid_range_for_this_index"}] /* range_scan_alternatives */,"analyzing_roworder_intersect": {"usable": false,"cause": "too_few_roworder_scans"} /* analyzing_roworder_intersect */} /* analyzing_range_alternatives */,"chosen_range_access_summary": {"range_access_plan": {"type": "range_scan","index": "PRIMARY","rows": 48962,"ranges": ["100000 < id"] /* ranges */} /* range_access_plan */,"rows_for_plan": 48962,"cost_for_plan": 4911.7,"chosen": true} /* chosen_range_access_summary */} /* range_analysis */}] /* rows_estimation */},{"considered_execution_plans": [{"plan_prefix": [] /* plan_prefix */,"table": "`user`","best_access_path": {"considered_access_paths": [{"rows_to_scan": 48962,"access_type": "range","range_details": {"used_index": "PRIMARY"} /* range_details */,"resulting_rows": 48962,"cost": 9807.9,"chosen": true}] /* considered_access_paths */} /* best_access_path */,"condition_filtering_pct": 100,"rows_for_plan": 48962,"cost_for_plan": 9807.9,"chosen": true}] /* considered_execution_plans */},{"attaching_conditions_to_tables": {"original_condition": "((`user`.`id` > 100000) and (`user`.`age` > 30))","attached_conditions_computation": [] /* attached_conditions_computation */,"attached_conditions_summary": [{"table": "`user`","attached": "((`user`.`id` > 100000) and (`user`.`age` > 30))"}] /* attached_conditions_summary */} /* attaching_conditions_to_tables */},{"finalizing_table_conditions": [{"table": "`user`","original_table_condition": "((`user`.`id` > 100000) and (`user`.`age` > 30))","final_table_condition   ": "((`user`.`id` > 100000) and (`user`.`age` > 30))"}] /* finalizing_table_conditions */},{"refine_plan": [{"table": "`user`"}] /* refine_plan */}] /* steps */} /* join_optimization */},{"join_execution": {"select#": 1,"steps": [] /* steps */} /* join_execution */}] /* steps */
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/599650.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux学习:awk和RE

awk awk是一种用于处理文本的编程语言工具&#xff0c;他扫描文件中的每一行&#xff0c;查找与命令行中所给定内容相匹配的模式&#xff0c;如果发现匹配内容&#xff0c;则进行下一个编程步骤。如果找 不到匹配内容&#xff0c;则继续处理下一行 awk ‘条件 1 {动作 1} 条件…

Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks

原文链接&#xff1a;https://arxiv.org/abs/1908.10084 提出契机&#xff1a; 提升相似文本的检索速度 在自然语言处理领域&#xff0c;BERT&#xff08;Bidirectional Encoder Representations from Transformers&#xff09;和RoBERTa&#xff08;A Robustly Optimized B…

网络安全之代码签名证书申请

代码签名&#xff0c;作为一种数字安全机制&#xff0c;对于软件开发、分发及用户使用环节具有至关重要的意义。以下从六大方面阐述代码签名必不可少的重要性&#xff1a; 确保代码来源可信&#xff1a; 代码签名如同软件的“身份证”&#xff0c;通过数字证书对开发者身份进…

CVE-2024-3148 DedeCms makehtml_archives_action sql注入漏洞分析

DedeCMS&#xff08;也称为织梦CMS&#xff09;是一款基于PHPMySQL的开源内容管理系统。 在 DedeCMS 5.7.112 中发现一个被归类为严重的漏洞。此漏洞会影响某些未知文件dede/makehtml_archives_action.php的处理。操作导致 sql 注入。攻击可能是远程发起的。该漏洞已向公众披露…

4.网络编程-websocket(golang)

目录 什么是websocket golang中使用websocket Server端 Client端 什么是websocket WebSocket是一种在互联网上提供全双工通信的协议&#xff0c;即允许服务器和客户端之间进行双向实时通信的网络技术。它是作为HTML5的一部分标准化的&#xff0c;旨在解决传统HTTP协议在实…

【Frida】【Android】 10_爬虫之WebSocket协议分析

&#x1f6eb; 系列文章导航 【Frida】【Android】01_手把手教你环境搭建 https://blog.csdn.net/kinghzking/article/details/136986950【Frida】【Android】02_JAVA层HOOK https://blog.csdn.net/kinghzking/article/details/137008446【Frida】【Android】03_RPC https://bl…

【C#】yield使用

&#x1f4bb;代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks;namespace ConsoleApp15 {internal class Program{static void Main(string[] args){IEnumerable<int&…

【微服务】面试题(一)

最近进行了一些面试&#xff0c;这几个问题分享给大家 一、分别介绍一下微服务、分布式以及两者的区别 微服务&#xff08;Microservices&#xff09;和分布式系统&#xff08;Distributed Systems&#xff09;是两种不同的软件架构风格&#xff0c;虽然它们之间有些重叠&#…

uniapp极光推送、java服务端集成

一、准备工作 1、进入【服务中心】-【开发者平台】 2、【创建应用】&#xff0c;填写应用名称和图标&#xff08;填写项目名称&#xff0c;项目logo就行&#xff0c;也可填写其他的&#xff09; 3、选择【消息推送】服务&#xff0c;点击下一步 ​ ​ Demo测试 参照文档&…

Stable Diffusion介绍

Stable Diffusion是一种前沿的开源深度学习模型框架&#xff0c;专门设计用于从文本描述生成高质量的图像。这种称为文本到图像生成的技术&#xff0c;利用了大规模变换器&#xff08;transformers&#xff09;和生成对抗网络&#xff08;GANs&#xff09;的力量&#xff0c;以…

css伪类:last-child或:first-child不生效

目录 一、问题 二、原因及解决方法 三、总结 tiips:如嫌繁琐&#xff0c;直接移步总结即可&#xff01; 一、问题 1.想使用伪类:last-child给 for循环出来的最后一个元素单独添加样式。但是发现无论怎么写都没有添加上去。 2.真是奇怪呀&#xff0c;明明写的没有问题呀&a…

【Linux ARM 裸机】开发环境搭建

1、Ubuntu 和 Windows 文件互传 使用过程中&#xff0c;要频繁进行 Ubuntu 和 Windows 的文件互传&#xff0c;需要使用 FTP 服务&#xff1b; 1.1、开启 Ubuntu 下的 FTP 服务 //安装 FTP 服务 sudo apt-get install vsftpd //修改配置文件 sudo vi /etc/vsftpd.conf//重启…