在海量数据中创建高效SQL笔记 参加了郑保卫博士的讲座,收获颇多。 以前总是写一些简单的sql,以为sql只是简单的命令,实际上sql大有可为,sql是体现技术人员能力的重要方面,sql是应用程序不是查询语言!以下简单总结了昨天培训的收获: www.2cto.com 1.SQL
在海量数据中创建高效SQL笔记
参加了郑保卫博士的讲座,收获颇多。 以前总是写一些简单的sql,以为sql只是简单的命令,实际上sql大有可为,sql是体现技术人员能力的重要方面,sql是应用程序不是查询语言!以下简单总结了昨天培训的收获:
www.2cto.com
1.SQL语句是一种集合语言,而JAVA /c++甚至于PL/SQL是过程化语言,SQL语句按block处理数据,过程化语言按条处理,效率差距可想而知。
2.扩张性的SQL语句可以做所有的有关数据处理的功能,不要再将SQL语句只当做一个数据库命令,“他是巨人,不是普通人”。
3.索引非常重要,建立战略性的索引是很困难的,最好建立联合索引,除非是主键,最好不要建立单独索引。索引的建立不是越多越好,也不是越少越好,应该有一个平衡点。因为索引也需要耗费存储空间,在delete/update特别是insert的时候过于臃肿的索引会影响语句的执行效率。
4.执行计划非常重要,要有执行计划和优化器的概念,对于一个SQL语句,要能够大概判断其执行的路径是什么。
www.2cto.com
5.执行速度很大程度上和IO有关,因为CPU的计算速度已经很快了,但I/O是很大的瓶颈。所以正在研究的有“内存数据库”的概念。
6.重视统计信息的作用,优化器是根据统计信息来进行执行计划的选择和优化的,统计信息可以定时让数据库收集、也可以在大量修改数据后手工执行统计信息的更新。
7.新的优化器是基于成本的,不是基于规则的。
8.调整数据库参数能解决10%的性能问题,修改执行计划可以解决30%,建立战略性索引和优化SQL语句可以解决50%的问题,而良好的数据模型和建模可以解决80%的性能问题。
9.执行计划是非常敏感的,微小的改动可能都会改变执行计划,比如SQL语句中表连接的数据、更新的统计信息、索引的改变等。
10.离散度的好坏决定索引效果的好坏,一般认为,如果某列数据的离散度低于10%-15%时,索引是有效的。
在SQL语句where查询条件中,有的条件用户驱动查询条件、有的用于过滤查询条件。最理想的方法是将拥有最小数据范围的条件作为查询驱动条件。
11.组合索引中列的顺序有可能决定查询效率。
12.索引的使用会受到表的联系方法和联系顺序的影响的。
13.PL/SQL中判断是否存在的替换方案:
原SQL:SELECT COUNT(*) INTO :CNT
FROM ITEM_TAB
WHERE DEPT = ‘101’
AND SEQ > 100;
…………………………………………
www.2cto.com
IF CNT > 0 ….
改进后:SELECT 1 INTO :CNT FROM DUAL
WHERE EXISTS (SELECT ‘X’
FROM ITEM_TAB
WHERE DEPT = ’101’
AND SEQ > 100 );
……………………………………………
IF CNT > 0 ….
14.取最大流水号的替代方案
原SQL:SELECT MAX(seq) + 1
FROM order
WHERE deptno = ‘12300’
替代SQL:SELECT /*+ INDEX_DESC(order pk_order) */
NVL(MAX(seq), 0) + 1
FROM order
WHERE deptno = ‘12300’
AND ROWNUM = 1
www.2cto.com
15.综合原理和总结
1) 数据处理就是集合运算;
2) 要从流程开发人员转变为数据开发人员,使用复杂的SQL,复杂的执行计划来替代过程化的程序执行,可以有效的降低计算次数,配合战略性索引,可以精简代码,提到性能。
3) SQL并非命令语句,而是应用程序!

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool