为sql server进行表分区 最近项目中数据量超过的千万级别,一个普通单表查询也慢的要命,对客户来说长时间的等待是不能容忍的。于是想尽各种办法对数据库进行优化,包括索引,单表查询等等。但结果都不近人意。最终选择对单表进行分区。经过测试,效率确实提
为sql server进行表分区
最近项目中数据量超过的千万级别,一个普通单表查询也慢的要命,,对客户来说长时间的等待是不能容忍的。于是想尽各种办法对数据库进行优化,包括索引,单表查询等等。但结果都不近人意。最终选择对单表进行分区。经过测试,效率确实提高了不少。
创建分区表一般包括四个步骤。
1:为分区创建文件组和文件
2:设计分区函数
3:设计分区架构
4:创建分区表
(1) 创建文件组和文件可以用sql创建,也可以通过管理工具创建具体方法如下:
1:管理工具:
打开Management Studio,选择要操作的数据库右键-属性-文件/文件组。根据相应的提示创建即可
2:通过sql创建
创建文件组:ALTER DATABASE Mytest ADD FILEGROUP MytestFileGroup ,其中Mytest是当前操作的数据库,MytestFileGroup是文件组名称
创建文集并将文件添加到文件组:
ALTER DATABASE Mytest ADD FILE
(
FILEGROWTH=1MB,
NAME='Mytest0' ,
FILENAME='C:\data\Mytest0.NDF',
SIZE=3MB
)TO FILEGROUP MytestFileGroup
通过一上操作,我们已经创建了一个名为MytestFileGroup的文件组以及文件组下面的文件Mytest0.NDF,文件的存放位置在c盘的data文件夹下。可以将不同文件存放 在不同的磁盘下面,这样可以提高IO效率,增加查询速度。
(2) 创建完成文件组和文件之后我们便可以设计分区函数了。直接上sql:
CREATE PARTITION FUNCTION MytestPartFunction (INT)
AS RANGE RIGHT
FOR VALUES
(2000000,4000000,6000000,8000000,10000000,12000000,14000000,16000000,18000000,20000000)
其中MytestPartFunction是函数名称。RANGE RIGHT表示分区的边界处理方式,这里RIGHT表示以右边界为准,既边界值分到右边的分区中。
分区依据是根据INT类型的值。
(3)创建完分区函数后,就要进行分区架构设计了。分区架构根据分区函数将不同的分区对应到不同的文件组。以达到不同分区数据存放到不同文件。分区架构代码如下:
CREATE PARTITION SCHEME MytestPartFunction
AS PARTITION MytestPartFunction
TO
(MytestFileGroup0,MytestFileGroup1,MytestFileGroup2,MytestFileGroup3,MytestFileGroup4,MMytestFileGroup5,MytestFileGroup6,MytestFileGroup7,MytestFileGroup8,MytestFileGroup9,MytestFileGroup10)
MytestPartFunction 是分区架构名称。MytestPartFunction是刚刚创建的分区函数名称,这里表示分区架构是以该函数进行分区。后面的MytestFileGroup0-MytestFileGroup10分别代表将不同的区域内的数据存放到不同的文件组。
(4) 创建完成分区函数和分区架构之后,就可以创建分区表了。创建分区表跟创建普通的表差不多。这里用代码实现如下:
create table UserInfo (
ID int identity,
UserCode nvarchar(50) not null,
UserName nvarchar(50) not null
) ON MytestPartFunction(ID)
后面的MytestPartFunction就是分区架构。其中括号中的ID表示以ID作为分区依据。
这样一个完整的分区表就完成了。其中有些细节没有说明。以后慢慢完善吧。本人对数据库了解的不太多,大部分东西都是从网上现找的,可能其中有些不足。恳请大家指正。
posted on

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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

Dreamweaver Mac version
Visual web development tools