MySql,Sql Server分区技术浅析
一.MySqlMySql在5.1以后的版本中加入了分区技术,其不同于以往的分表技术,之前的分表技术是把一张大的表水平(按照一定的逻辑)分成多张表,比如如果我们的User
一.MySql
MySql在5.1以后的版本中加入了分区技术,其不同于以往的分表技术,之前的分表技术是把一张大的表水平(按照一定的逻辑)分成多张表,比如如果我们的User表中有1000万条数据,那如果放在一张表里面去查询,绝对是坑爹的行为,更别提再去进行增删改。如果在加上索引,撑爆内存是难免的。所以才有了之后的分表技术,比如把用户名是a开头的用户放入一张表里面,这样可以减少该表的数据量 ,但是同样这个在应用层上也需要对系统进行优化,比如当我查询“abc”这个用户时,我知道他的信息在user1这个表里,然后与之对应的select语句就要发生相应的变化。当然进行水平分表时也不一定仅仅按照用户名的首字母来匹配对应相应的存储表,应根据信息得不同建立相应的逻辑对应关系。发现自己扯得有点远……。咱们回过头来说一下MySql5.1版本之后的的分区技术,不过还要提一下之前的分表技术,多大表进行拆分后,其相应的子表(暂且这么称呼)在逻辑上是变化的,这就体现在我们查询sql语句的不同上,那有没有一种技术可以在逻辑上保持原状,仅仅在物理结构上发生变化呐?这就是我们要提到的MySql的分区技术。对应用程序而言,他还是一张表,这样可以在逻辑层上屏蔽我们之前遇到的复杂查询语句。
MySql5.1上有5种分区类型,下面就让我们一个个的来瞅瞅看:
1)RANGE分区(经常使用):
基于属于一个给定连续区间的列值,把多个行分配给分区;
例:假定你创建了一个如下表,该表保存了20家音像店的职员记录,这20家音像店的编号从1到20,你想把不同时期离职的职员的信息分别存储,那么你可以将字段separated(即离职时间)作为一个Key,则sql语句如下:
则LIST分区语句为:
相信大家都看得懂,不做过多的解释。
3)HASH分区(较少使用)
基于用户定义的表达式的返回值来进行选择的分区,该表达式使用将要插入到表中的那些行的列值进行计算,这个函数可以包含MySql重的有效的、产生非负整数值的任何表达式。其要根据该表所处的环境来衡量是否可用于该表,也就是在预先确定数目的分区中平均分布。
例:还是上面的那种表,那么现在我想把不同时期入职的员工分别进行存储,那我可以将日期字段Hired作为一个Key,sql语句如下:
CREATE TABLE Employees( Id INT NOT NULL, Fname VARCHAR(30), Iname VARCHAR(30), Hired DATE NOT NULL DEFAULT ‘1990-01-01’, Separated DATE NOT NULL DEFAULT ‘9999-12-31’, Job_CODE INT, Store_ID INT ) PARTITION BY HASH(YEAR(Hired)) PARTITIONS 4 ;也就是说根据Hired这个字段把数据平均分配到4个不同分区表中。注意:HASH中的值必须是整数所以使用到了YEAR函数。
4)KEY分区(很少使用)
类似于HASH分区,区别在于KEY分区只提供计算一列或多列,且MySql服务器提供其自身的哈希函数。与HASH不同的是它的Key可以不是整数类型,可以是字符串等字段,该分区使用不多,而且效率有些折扣,在此不再举例;
二.Sql Server
Sql Server在2005之后的版本引入的特性。这个特性允许逻辑上的表在物理上分成多个部分,之前所谓的分区表仅仅是分布式视图,也就是多个表做union视图,网站空间,而真正的分区表是逻辑上一个表,香港虚拟主机,物理上多个表,原理跟MySql分区表的概念基本一致。有一点值得注意的是分区函数并不具体属于分区架构和分区表,他们之间仅仅属于使用关系。
1).定义分区表首先要定义分区函数,例如:
该函数把时间分成了3个区域,2010-01-01之前是一个区域,2010-01-01~2012-01-01是一个区域,剩下的是一个区域。
2).定义分区架构
定义完分区函数仅仅是到了如何将列的值区分到不同的分区中,而每个分区的存储方式则需要分区架构来定义,分区架构负责分配每个区属于那个文件组,而分区函数是决定了如何在逻辑上分区
3).定义分区表
这个就不用过多解释了,就是我们的逻辑表,只不过显示当中都是某张表够大的时候才考虑采用分区表,但是当我们在刚刚建立时需要指定相关的特性,服务器空间,示例代码:
CREATE TABLE PTable( ID INT, ORDERID INT, SALESDATE DATE ) ON schemeForPartition(SALESDATE) --schemeForPartition指定分区架构根据的Key为SALESDATE就创建完成了
这样Sql Server数据库的分区表就创建完成了。。。
今天先写到这里,以后在对分区表进行深入的研究。
本文出自 “跃跃欲试的鱼” 博客,谢绝转载!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

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.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

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
