经常我们需要将大表根据分区键进行分区,当建立索引的时候,我们到底使用local 还是global 索引呢 先看看两种索引的特点: 本地索引特点: 1. 本地索引一定是分区索引,分区键等同于表的分区键,分区数等同于表的分区说,一句话,本地索引的分区机制和表的分
经常我们需要将大表根据分区键进行分区,当建立索引的时候,我们到底使用local 还是global 索引呢
先看看两种索引的特点:
本地索引特点: 1. 本地索引一定是分区索引,分区键等同于表的分区键,分区数等同于表的分区说,一句话,本地索引的分区机制和表的分区机制一样。 2. 如果本地索引的索引列以分区键开头,则称为前缀局部索引。 3. 如果本地索引的列不是以分区键开头,或者不包含分区键列,则称为非前缀索引。 4. 前缀和非前缀索引都可以支持索引分区消除,前提是查询的条件中包含索引分区键。 5. 本地索引只支持分区内的唯一性,无法支持表上的唯一性,因此如果要用本地索引去给表做唯一性约束,则约束中必须要包括分区键列。 6. 本地分区索引是对单个分区的,每个分区索引只指向一个表分区,全局索引则不然,一个分区索引能指向 n个表分区,同时,一个表分区,也可能指向 n个索引分区,对分区表中的某个分区做 truncate或者 move, shrink等,可能会影响到 n个全局索引分区,正因为这点,本地分区索引具有更高的可用性。 7. 位图索引只能为本地分区索引。 8. 本地索引多应用于数据仓库环境中。 全局索引特点: 1.全局索引的分区键和分区数和表的分区键和分区数可能都不相同,表和全局索引的分区机制不一样。 2.全局索引可以分区,也可以是不分区索引,全局索引必须是前缀索引,即全局索引的索引列必须是以索引分区键作为其前几列。 3.全局分区索引的索引条目可能指向若干个分区,因此,对于全局分区索引,即使只截断一个分区中的数据,都需要 rebulid若干个分区甚至是整个索引。 4.全局索引多应用于 oltp系统中。 5.全局分区索引只按范围或者散列 hash分区, hash分区是 10g以后才支持。 6.oracle9i以后对分区表做move 或者truncate 的时可以用update global indexes语句来同步更新全局分区索引,用消耗一定资源来换取高度的可用性。 7.表用 a列作分区,索引用 b做局部分区索引,若 where条件中用 b来查询,那么 oracle会扫描所有的表和索引的分区,成本会比分区更高,此时可以考虑用 b做全局分区索引。 通常开发人员喜欢建立local索引,因为在删除分区的时候不需要重建索引。但是有时候本地索引将会带来很大的性能影响:----------------------------------------------------------------------------------------------------------------------- | Id | Operation |Name | Starts | E-Rows | A-Rows |Buffers |Pstart| Pstop | ----------------------------------------------------------------------------------------------------------------------- | 0 |SELECT STATEMENT | | 1 | | 1493 | 2984 | | | | 1 | PARTITION RANGEALL | | 1 | 1493 | 1493 | 2984 | 1 | 1493 | | 2 | TABLE ACCESS BY LOCAL INDEX ROWID| PARTITIONED_TAB | 1493 | 1493 | 1493 | 2984 | 1 | 1493 | |* 3 | INDEX RANGE SCAN | LC_NON_PREFIXED_TYP_I | 1492 | 1493 | 1493 | 1492 | 1 | 1493 | ----------------------------------------------------------------------------------------------------------------------- Predicate Information (identifiedby operation id): --------------------------------------------------- 3 - access(MHO_TYP_ID=0) Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 2984 consistent gets 0 physical reads 0 redosize 28937 bytes sent via SQL*Netto client 372 bytes received via SQL*Netfrom client 4 SQL*Net roundtripsto/fromclient 0 sorts (memory) 0 sorts (disk) 1493 rows processedLC_NON_PREFIXED_TYP_I执行了1942次,这种情况如果我们加上适当的条件,尽量减少分区的扫描:
SQL>select * from partitioned_tab wheremho_typ_id = 0 and mho_date = to_date('01122012','ddmmyyyy'); MHO_ID MHO_DATE M MHO_TYP_ID ---------- ----------------- - ---------- 1 20121201 00:00:00 Z 0 ------------------------------------------------------------------------------------------------------------------------- | Id | Operation |Name | Starts | E-Rows | A-Rows | Buffers | Pstart| Pstop | ------------------------------------------------------------------------------------------------------------------------- | 0 |SELECT STATEMENT | | 1 | | 1 | 2 | | | | 1 | PARTITION RANGE SINGLE | | 1 | 1 | 1 | 2 | 2 | 2 | |* 2 | TABLE ACCESS BY LOCAL INDEX ROWID| PARTITIONED_TAB | 1 | 1 | 1 | 2 | 2 | 2 | |* 3 | INDEX RANGE SCAN | LC_NON_PREFIXED_TYP_I | 1 | 1 | 1 | 1 | 2 | 2 | ------------------------------------------------------------------------------------------------------------------------- Predicate Information (identifiedby operation id): --------------------------------------------------- 2 - filter(MHO_DATE=TO_DATE(' 2012-12-01 00:00:00','syyyy-mm-dd hh24:mi:ss')) 3 - access(MHO_TYP_ID=0) 这次LC_NON_PREFIXED_TYP_I只执行了一次 那我们换成global index呢?
SQL>select * from partitioned_tab where mho_typ_id = 0; ------------------------------------------------------------------------------------------------------------------ | Id | Operation |Name | Starts | E-Rows | A-Rows |Buffers | Pstart| Pstop | ------------------------------------------------------------------------------------------------------------------ | 0 |SELECT STATEMENT | | 1 | | 1493 | 1496 | | | | 1 | TABLE ACCESS BY GLOBAL INDEX ROWID| PARTITIONED_TAB | 1 | 1493 | 1493 | 1496 | ROWID | ROWID | |* 2 | INDEX RANGE SCAN | GL_TYP_I | 1 | 1493 | 1493 | 4 | | | ------------------------------------------------------------------------------------------------------------------ Predicate Information (identifiedby operation id): --------------------------------------------------- 2 - access(MHO_TYP_ID=0) Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 1496 consistent gets 1493 physical reads 0 redosize 28937 bytes sent via SQL*Netto client 372 bytes received via SQL*Netfrom client 4 SQL*Net roundtripsto/fromclient 0 sorts (memory) 0 sorts (disk) 1493 rows processed索引只执行了一次,consistent gets为1496 是用local的一半。索引建立global或local index 我们需要根据分区数量、分区稳定性、sql语句综合考虑。

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


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

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

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

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
