MSSQL table sharding query (01)

Source: Internet
Author: User

In the MSSQL Stored Procedure, table sharding is implemented to store table sharding queries and improve query efficiency.

Table sharding and table sharding for some fixed data can greatly improve the query performance. This is already accepted. The following describes how to implement it in the storage process.

If you want to query the region of a mobile phone number, the database of the mobile phone number is saved to the 10 tables of M0 to M9 respectively. Note: only the first seven digits of the mobile phone number are saved in the table, the first seven digits can be used to determine the region!

Alter proc [DBO]. [p-searchmobile]

@ Mobile bigint,

@ Type int output,

@ Province varchar (6) output,

@ City varchar (24) output,

@ Info varchar (30) Output

As

Declare @ SQL nvarchar (200)

Declare @ number int

Set @ number = cast (left (@ mobile, 7) as INT) -- retrieve the first seven digits

-- Note that table store uses the 7th-bit number of the mobile phone number to determine which table exists.

Set @ SQL = 'select top 1 @ type = [type], @ city = city, @ province = province, @ info = info

From [m' + Cast (@ Number % 10 as varchar (10) + ']

Where number = '+ Cast (@ number as varchar (10 ))

 

Exec sp_executesql @ SQL, n' @ Type int output,

@ City varchar (24) output,

@ Province varchar (6) output,

@ Info varchar (30) output', -- the four output variables here replace the corresponding variables in the @ SQL string

-- The following four values are used to receive the values of four variables.

@ Type output,

@ City output,

@ Province output,

@ Info output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.