search
HomeDatabaseMysql TutorialSQLServer分布式查询(2)

SQLServer分布式查询(2)

Jun 07, 2016 pm 03:27 PM
sqlserverdistributedtroubleshootingInquirequestion

3问题排查与更多查询方式 当我们在实际编程中进行访问远程数据时 因为不同操作环境会引发各种各样的异常,如下我会提出一种常见的异常方式解决办法和关于远程数据操作更多查询方式. 3.1无法建立远程连接 其实这个问题在做分布式查询时极其常见. 而引起这个问

  问题排查与更多查询方式

  当我们在实际编程中进行访问远程数据时 因为不同操作环境会引发各种各样的异常,如下我会提出一种常见的异常方式解决办法和关于远程数据操作更多查询方式.

  无法建立远程连接

  其实这个问题在做分布式查询时极其常见. 而引起这个问题的因素过多. 我们一时无法判断真正引发这个异常地方. 只能通过逐个排查方式来进行设置:

  例如我们在建立关联关系后 进行查询时会遇到:

SQLServer分布式查询(2)

  提示是: 在进行远程连接时超时, 引起这个问题原因可能是远程服务器积极拒绝访问!

  首先要在Sql Server Configuation Manager中保证你服务已经运行 且是开机自动运行.

  再次检查SQl2005外围配置DataBaseEngine允许远程连接:

SQLServer分布式查询(2)

  设置完成后.我们还需要设置Sql Server Analysis Services分析服务也支持远程数据查询:

SQLServer分布式查询(2)

  在远程服务器上如果启用了防火墙则可能对目前SQl Server方位实例进行拦截. 所以在服务器端启用防火墙情况下要为SQl DAtaBase创建例外.防止客户端请求被拦截.

  进程被其他用户占用

  当我们在远程分布式查询中有创建动作或是类似创建一个新的数据库. 有时会提示 “该数据库无法操作 已经别其他进程占用”异常. 导致我们无法访问数据库. 或是执行我们要做的创建操作.

  遇到这种情况我们可以利用SA权限查询到Master数据库对应数据库被占用的进程 并杀掉Kill Process.查询:

<p><span>1</span><span>:  </span><span>--</span><span> [sysprocesses 表中保存关于运行在 Microsoft® SQL Server™ 上的进程的信息。</span><span><br>   </span><span>2</span><span>:  </span><span>--</span><span> 这些进程可以是客户端进程或系统进程。sysprocesses 只存储在 master 数据库中]</span><span><br></span><span>4</span><span>:  </span><span>use</span><span> Master<br>   </span><span>5</span><span>:  </span><span>go</span><span><br>   </span><span>7</span><span>:  </span><span>SELECT</span><span>*</span><span>FROM</span><span> sysprocesses ,sysdatabases </span><span>WHERE</span><span> sysprocesses.dbid</span><span>=</span><span>sysdatabases.dbid </span><span>AND</span><span> sysdatabases.Name</span><span>=</span><span>'</span><span>CustomerDB</span><span>'</span><span><br>   </span><span>9</span><span>:  </span><span>select</span><span>*</span><span>from</span><span> sysprocesses<br>  </span><span>11</span><span>:  </span><span>select</span><span>*</span><span>from</span><span> sysdatabases<br>  </span><span>13</span><span>:  </span><span>--</span><span> 杀死占用进程</span><span><br></span><span>14</span><span>:  </span><span>kill</span><span>5</span></p>

  当我们对进程占用清除时有可能访问数据库被系统进程占用. 则这时用Sa无法杀死.这时提示:

SQLServer分布式查询(2)

  “Only use Process can be Kill ”在SQl2005 只有只有用户进程才能Kill掉.

  更多的查询操作

  往往我们在实际操作中需要对数据读写有更多要求. 例如从远程连接多个服务器进行数据读取或是把本地数据提交到服务器上. 为了提高效率和性能采用分布式事务来进行批量操作等等. 如下简单介绍在分布式查询中多中数据操作:

  把远程数据导入本地:

<p><span>1</span><span>:  </span><span>--</span><span> 导入数据操作</span><span><br>   </span><span>2</span><span>:  </span><span>select</span><span>top</span><span>(</span><span>3</span><span>) </span><span>*</span><span>into</span><span> TestDB.dbo.CopyDb </span><span>from</span><span>[</span><span>192.168.10.76</span><span>]</span><span>.wl.dbo.Users<br></span></p>

  导入时使用Into方式 自动在本地创建CopyDB表完全复制远程服务器上Users表的数据结构.但是要注意在进行后 的CopyDB将不包含原表的主键和索引约束. 虽然能快构建 但是主键和索引设置都会丢失.

  本地数据导入远程:

<p><span>--</span><span> 把本地表导入远程表 [openWset方式]</span><span><br></span><span>insert</span><span>openrowset</span><span>( </span><span>'</span><span>SQLOLEDB </span><span>'</span><span>, </span><span>'</span><span>sql服务器名 </span><span>'</span><span>; </span><span>'</span><span>用户名 </span><span>'</span><span>; </span><span>'</span><span>密码 </span><span>'</span><span>,数据库名.dbo.表名)  </span><span>select</span><span>*</span><span>from</span><span> 本地表 <br></span><span>--</span><span> 把本地表导入远程表 [open Query方式]</span><span><br></span><span>insert</span><span>openquery</span><span>(ITSV, </span><span>'</span><span>SELECT * FROM 数据库.dbo.表名 </span><span>'</span><span>)  </span></p>
更新本地表数据:<br>

<p><span>1</span><span>:  </span><span>--</span><span> 把本地表导入远程表 [opendataSource方式]</span><span><br>   </span><span>2</span><span>:    </span><span>insert</span><span>opendatasource</span><span>( </span><span>'</span><span>SQLOLEDB </span><span>'</span><span>, </span><span>'</span><span>Data Source=ip/ServerName;User ID=登陆名;Password=密码 </span><span>'</span><span>).数据库.dbo.表名  <br>   </span><span>3</span><span>:   <br>   </span><span>4</span><span>:  </span><span>--</span><span> 更新本地表 [openowset方式]</span><span><br></span><span>5</span><span>:   </span><span>update</span><span> b  </span><span>set</span><span> b.列A</span><span>=</span><span>a.列A  </span><span>from</span><span>openrowset</span><span>( </span><span>'</span><span>SQLOLEDB </span><span>'</span><span>, </span><span>'</span><span>sql服务器名 </span><span>'</span><span>; </span><span>'</span><span>用户名 </span><span>'</span><span>; </span><span>'</span><span>密码 </span><span>'</span><span>,数据库名.dbo.表名)<br>   </span><span>6</span><span>:   </span><span>as</span><span> a </span><span>inner</span><span>join</span><span> 本地表 b  </span><span>on</span><span> a.column1</span><span>=</span><span>b.column1  </span></p>

  当然还有更多方式来操作分布式查询操作.各位都可以尝试.

  尾 语

  如上是我最近在项目中处理关于分布式查询涉及到方方面面. 从系统架构到分部是查询具体操作细节.基本都是一些非常基础运用.当然也参考不少资料.以及动手来验证整个过程出现问题原因所在. 篇幅有限 写的有些仓促. 难免有纰漏地方 还望各位指正.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

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.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor