SQL Server DO's and DON'Ts

本文为 SQL Server 项目的负责人提供了一系列实用的设计建议,包括避免使用游标、使用参数化查询、进行表规范化等最佳实践,旨在帮助提升数据库性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SQL Server DO's and DON'Ts

So, you are now the leader of a SQL Server based project and this is your first one, perhaps migrating from Access. Or maybe you have performance problems with your SQL Server and don't know what to do next. Or maybe you simply want to know of some design guidelines for solutions using SQL Server and designing Database Access Layers (DAL): this article is for you.

Even if you are not using SQL Server, most of these design guidelines apply to other DBMS, too: Sybase is a very similar environment for the programmer, and Oracle designs may benefit from this too. I won't show here how to use specific T-SQL tricks, nor won't give you miracle solutions for your SQL Server problem. This is by no means a complete, closed issue. What I intend to do is give you some advices for a sound design, with lessons learned through the last years of my life, seeing the same design errors being done again and again.

DO know your tools.

Please, don't underestimate this tip. This is the best of all of those you'll see in this article. You'd be surprised of how many SQL Server programmers don't even know all T-SQL commands and all of those effective tools SQL Server has.

"What? I need to spend a month learning all those SQL commands I'll never use???" you might say. No, you don't need to. But spend a weekend at MSDN and browse through all T-SQL commands: the mission here is to learn a lot of what can and what can't be done. And, in the future, when designing a query, you'll remember "Hey, there's this command that does exactly what I need", and then you'll refer again to MSDN to see its exact syntax.

In this article I'll assume that you already know the T-SQL syntax or can find about it on MSDN.

DON'T use cursors

Let me say it again: DON'T use cursors. They should be your preferred way of killing the performance of an entire system. Most beginners use cursors and don't realize the performance hit they have. They use memory; they lock tables in weird ways, and they are slow. Worst of all, they defeat most of the performance optimization your DBA can do. Did you know that every FETCH being executed has about the same performance of executing a SELECT? This means that if your cursor has 10,000 records, it will execute about 10,000 SELECTs! If you can do this in a couple of SELECT, UPDATE or DELETE, it will be much faster.

Beginner SQL programmers find in cursors a comfortable and familiar way of coding. Well, unfortunately this lead to bad performance. The whole purpose of SQL is specifying what you want, not how it should be done.

I've once rewritten a cursor-based stored procedure and substituted some code for a pair of traditional SQL queries. The table had only 100,000 records and the stored procedure used to take 40 minutes to process. You should see the face of the poor programmer when the new stored procedure took 10 seconds to run!

Sometimes it's even faster to create a small application that gets all the data, proccess it and update the server. T-SQL was not done with loop performance in mind.

If you are reading this article, I need to mention: there is no good use for cursors; I have never seen cursors being well used, except for DBA work. And good DBAs, most of the time, know what they are doing. But, if you are reading this, you are not a DBA, right?

DO normalize your tables

There are two common excuses for not normalizing databases: performance and pure laziness. You'll pay for the second one sooner or later; and, about performance, don't optimize what's not slow. Often I see programmers de-normalizing databases because "this will be slow". And, more frequent than the inverse, the resulting design is slower. DBMSs were designed to be used with normalized databases, so design with normalization in mind.

DON'T SELECT *

This is hard to get used, I know. And I confess: often I use it; but try to specify only the columns you'll need. This will:

  1. Reduce memory consumption and network bandwidth
  2. Ease security design
  3. Gives the query optimizer a chance to read all the needed columns from the indexes

DO know how your data will be/is being acessed

A robust index design is one of the good things you can do for your database. And doing this is almost an art form. Everytime you add an index to a table, things get faster on SELECT, but INSERT and DELETE will be much slower. There's a lot of work in building and mantaining indexes. If you add several indexes to a table to speed your SELECT, you'll soon notice locks being held for a long time while updating indexes. So, the question is: what is being done with this table? Reading or Updating data? This question is tricky, specially with the DELETE and UPDATE, because they often involve a SELECT for the WHERE part and after this they update the table.

DON'T create an index on the "Sex" column

This is useless. First, let's understand how indexes speed up table access. You can see indexes as a way of quickly partitioning a table based on a criteria. If you create an index with a column like "Sex", you'll have only two partitions: Male and Female. What optimization will you have on a table with 1,000,000 rows? Remember, mantaining an index is slow. Always design your indexes with the most sparse columns first and the least sparse columns last, e.g, Name + Province + Sex.

DO use transactions

Specially on long-running queries. This will save you when things get wrong. Working with data for some time you'll soon discover some unexpected situation which will make your stored procured crash.

DO beware of deadlocks

Always access your tables on the same order. When working with stored procedures and transactions, you may find this soon. If you lock the table A then table B, always lock them in this very same order in all stored procedures. If you, by accident, lock the table B and then table A in another procedure some day you'll have a deadlock. Deadlocks can be tricky to find if the lock sequence is not carefully designed.

DON'T open large recordsets

A common request on programming forums is: "How can I quickly fill this combo with 100,00 items?". Well, this is an error. You can't and you shouldn't. First, your user will hate browsing through 100,000 records to find the right one. A better UI is needed here, because you should ideally show no more that 100 or 200 records to your users.

DON'T use server side cursors

Unless you know what your are doing. Client side cursors often (not always) put less overhead on the network and on the server, and reduce locking time.

DO use parametrized queries

Sometimes I see in programming forums, questions like: "My queries are failing with some chars, e.g. quotes. How can I avoid it?". And a common answer is: "Replace it by double quotes". Wrong. This is only a workaround and will still fail with other chars, and will introduce serious security bugs. Besides this, it will trash the SQL Server caching system, which will cache several similar queries, instead of caching only one. Learn how to use parameterized queries (in ADO, through the use of the Command Object, or in ADO.NET the SqlCommand) and never have these problems again.

DO always test with large databases

It's a common pattern programmers developing with a small test database, and the end user using large databases. This is an error: disk is cheap, and performance problems will only be noticed when it's too late.

DON'T import bulk data with INSERT

Unless strictly necessary. Use DTS or the BCP utility and you'll have both a flexible and fast solution.

DO beware of timeouts

When querying a database, the default timeout is often low, like 15 seconds or 30 seconds. Remember that report queries may run longer than this, specially when your database grows.

DON'T ignore simultaneous editing

Sometimes two users will edit the same record at the same time. When writing, the last writer wins and some of the updates will be lost. It's easy to detect this situation: create a timestamp column and check it before you write. If possible, merge changes. If there is a conflict, prompt the user for some action.

DON'T do SELECT max(ID) from Master when inserting in a Detail table.

This is another common mistake, and will fail when two users are inserting data at the same time. Use one of SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY. Avoid @@IDENTITY if possible because it can introduce some nasty bugs with triggers.

DO Avoid NULLable columns

When possible. They consume an extra byte on each NULLable column in each row and have more overhead associated when querying data. The DAL will be harder to code, too, because everytime you access this column you'll need to check

I'm not saying that NULLs are the evil incarnation, like some people say. I believe they can have good uses and simplify coding when "missing data" is part of your business rules. But sometimes NULLable columns are used in situations like this:

CustomerName1
CustomerAddress1
CustomerEmail1
CustomerName2
CustomerAddress2
CustomerEmail3
CustomerName1
CustomerAddress2
CustomerEmail3

This is horrible. Please, don't do this, normalize your table. It will be more flexible and faster, and will reduce the NULLable columns.

DON'T use the TEXT datatype

Unless you are using it for really large data. The TEXT datatype is not flexible to query, is slow and wastes a lot of space if used incorrectly. Sometimes a VARCHAR will handle your data better.

DON'T use temporary tables

Unless strictly necessary. Often a subquery can substitute a temporary table. They induce overhead and will give you a big headache when programming under COM+ because it uses a database connection pool and temporary tables will last forever. In SQL Server 2000, there are alternatives like the TABLE data type which can provide in-memory solutions for small tables inside stored procedures too.

DO learn how to read a query execution plan

The SQL Server query analyzer is your friend, and you'll learn a lot of how it works and how the query and index design can affect performance through it.

DO use referential integrity

This can be a great time saver. Define all your keys, unique constraints and foreign keys. Every validation you create on the server will save you time in the future.

Conclusion

As I've said before, this is by no means a complete SQL Server performance and best practices guide. This would take a complete book to cover. But I really believe that this is a good start, and if you follow these practices, surely you will have much less trouble in the future.

 

License

解释以下脚本:@echo off&setlocal enabledelayedexpansion :: 日志路径 set var_date=%date:~0,10% set var_date=%var_date:-=% set log_path=%temp%\ibdata%var_date%.log echo ******* start process ******** echo. echo %time% start process >> %log_path% echo %log_path% pause :: 脚本的存放位置固定为 D:\naura echo %cd% pause if %cd% NEQ D:\naura ( echo %time% work path is %cd% >> %log_path% echo 请将脚本文件及ibdata1文件放在 D:\naura 目录下 echo. pause exit 0 ) :: 检查ibdata1 文件是否存在 if not exist ibdata1 ( echo ibdata1 文件不存在,请检查脚本文件所在目录 echo. pause exit 0 ) :: 检查ibdata1 文件是否存在 if not exist tables.txt ( echo tables.txt 文件不存在,请检查脚本文件所在目录 echo. pause exit 0 ) :: 校验 ibdata1 的大小是否符合预期 for /f %%i in ('dir /b ibdata1') do ( echo %time% lastest ibdata1 file size is %%~zi >> %log_path% if %%~zi NEQ 10485760 ( echo ibdata1 文件大小不对,请核对脚本目录下 ibdata1 文件 echo. pause exit 0 ) ) set excute_h=1 set excute_m=1 set excute_s=1 :: 是否需要手动干预的标志 set handwork=0 :: 检查数据库是否启动 同时检查是否有数据库操作权限 tasklist|find /i "mysqld.exe" >> %log_path% if %errorlevel% NEQ 0 ( echo %time% mysql is not running and started to start >> %log_path% net start mysql >> %log_path% 2>&1 if !errorlevel! NEQ 0 ( echo %time% failed to start mysql echo mysql 启动失败,请手动启动mysql服务之后,再执行脚本 echo. pause exit 0 ) ) else ( echo %time% verify that we have execute net permission >> %log_path% net help >> %log_path% 2>&1 if !errorlevel! NEQ 0 ( echo %time% we don't have authority >> %log_path% echo 由于权限问题,清理过程需要您手动干预,请不要离开机台,注意脚本运行!! echo. pause echo. set handwork=1 ) ) echo %time% begin excute clean mysql data echo. :: 定义变量,方便修改 set mysqlServer=localhost set mysqlUser=root set mysqlPassword=8888 set database_fa300=fa300 set database_scope=scope set curObj.table=0 set curObj.field=0 set export_ingnore_table=%database_scope% set dataSavePath=naura_scope set tryCount=0 :: 获取所有数据记录表,表中数据需要根据时间备份 set tableCounts=0 set obj=0 for /f "delims=: tokens=1-2" %%i in (tables.txt) do ( for /f "skip=1" %%a in ('mysql -h%mysqlServer% -u%mysqlUser% -p%mysqlPassword% -e "select count(0) from information_schema.tables where table_name='%%i' and table_schema='%database_scope%'"') do ( if %%a EQU 1 ( set obj[!tableCounts!].table=%%i set obj[!tableCounts!].field=%%j set /a tableCounts=!tableCounts!+1 ) ) ) :: 脚本执行过程中产生的临时参数保存路径 set process_param=%temp%\ibdata_param.log :: 临时数据保存的磁盘 set param_volume=0 :: 脚本执行到第几步 set param_step=0 :: 参数文件产生日期 set param_date=0 :: 备份天数 set param_day=0 :: ibdata 文件路径 set param_ibdataPath=0; :: 导出或导入数组 obj 的下标 set param_tableIndex=0; set begin_time=%time% if not exist %process_param% goto:step0 echo %time% %process_param% file exist >> %log_path% echo ----------------------- >> %log_path% type %process_param% >> %log_path% echo ----------------------- >> %log_path% for /f "delims== tokens=1-2" %%i in (%process_param%) do ( if %%i EQU param_date ( set param_date=%%j ) ) echo %time% param_date: %param_date% >> %log_path% :: 判断已经存在的参数,是否为同一天,默认同一天的可以继续使用 if %date:~0,10% NEQ %param_date% goto:step0 :: 获取所有的参数 for /f "delims== tokens=1-2" %%i in (%process_param%) do ( if %%i EQU volume ( set param_volume=%%j ) else if %%i EQU step ( set param_step=%%j ) else if %%i EQU day ( set param_day=%%j ) else if %%i EQU ibdataPath ( set param_ibdataPath=%%j ) else ( set param_tableIndex=%%j ) ) echo %time% param: %param_volume% %param_step% %param_day% %param_ibdataPath% %param_tableIndex%>> %log_path% if not exist "%param_ibdataPath%" goto:step0 goto:step%param_step% :: 环境准备 :: 1 获取临时数据存储位置 :: 2 获取 mysql data 的位置 :: 3 判断临时数据存储空间是否满足 :: 4 估算脚本运行时间 :step0 echo %time% begin excute step0 >> %log_path% del /Q/F %process_param% >> %log_path% 2>&1 :: 自动获取可用空间最大的非系统盘, 如果存在 E 盘,则直接默认 E 盘 set param_volume=0 set volume_freespace=0 for /f "Skip=2 tokens=1-2" %%i in ('Wmic LogicalDisk where "DriveType=3" Get FreeSpace^,Name') do ( echo %time% %%j %%i >> %log_path% set tempSize=%%i set /a tempSize=!tempSize:~0,-6! / 1074 > nul 2>&1 if %%j EQU E: ( set param_volume=%%j set /a volume_freespace=!tempSize! goto:endVolume ) if !tempSize! GTR !volume_freespace! ( set param_volume=%%j set /a volume_freespace=!tempSize! ) ) :endVolume echo %time% the path to save the temp data is %param_volume%, freespce is %volume_freespace%GB >> %log_path% if not exist %param_volume%\%dataSavePath% md %param_volume%\%dataSavePath% :: 获取 ibdata1 文件路径 echo %time% show variables datadir >> %log_path% mysql -h %mysqlServer% -u %mysqlUser% -p%mysqlPassword% -e "show variables like 'datadir'" > %param_volume%\%dataSavePath%\data.txt 2>>%log_path% type %param_volume%\%dataSavePath%\data.txt >> %log_path% for /f "delims= tokens=1" %%i in (%param_volume%\%dataSavePath%\data.txt) do ( echo %%i | findstr "Value" >nul && echo. >nul || set param_ibdataPath=%%i ) set param_ibdataPath=%param_ibdataPath:~8%ibdata1 set param_ibdataPath=%param_ibdataPath:\\=\% if not exist "%param_ibdataPath%" ( mysql -h %mysqlServer% -u %mysqlUser% -p%mysqlPassword% -e "show variables like 'innodb_data_home_dir'" > %param_volume%\%dataSavePath%\data.txt 2>>%log_path% type %param_volume%\%dataSavePath%\data.txt >> %log_path% for /f "delims= tokens=1" %%i in (%param_volume%\%dataSavePath%\data.txt) do ( echo %%i | findstr "Value" >nul && echo. >nul || set param_ibdataPath=%%i ) set param_ibdataPath=!param_ibdataPath:~21!ibdata1 set param_ibdataPath=!param_ibdataPath:\\=\! ) echo. echo !param_ibdataPath! echo ibdata1 file path !param_ibdataPath! >> %log_path% echo. if not exist "%param_ibdataPath%" ( echo 没有找到 ibdata 文件,请联系软件工程师!!!! echo. pause exit ) :: 获取客户端 ibdata file size set oldIbdataSize=0 for /f "tokens=3" %%i in ('dir "%param_ibdataPath%"^|find /i "1 个文件"') do ( set oldIbdataSize=%%i ) set oldIbdataSize=%oldIbdataSize:,=% echo %time% old ibdata file size is %oldIbdataSize% >> %log_path% :: 判断临时数据存储位置的空间是否够用 set /a oldIbdataSize=%oldIbdataSize:~0,-6% / 1074 echo %time% old ibdata file size is %oldIbdataSize%GB >> %log_path% set /a oldIbdataSize=%oldIbdataSize%+10 if %volume_freespace% LSS %oldIbdataSize% ( echo %param_volume% freespce is too small >> %log_path% echo %param_volume% 剩余空间太小,至少需要 %oldIbdataSize%GB 空间,请手动清理后再次运行脚本 echo. pause exit 0 ) set /a oldIbdataSize=%oldIbdataSize%*3 echo 整个清理过程大约需要 %oldIbdataSize% min echo. set param_step=1 call:fun_save_param echo %time% end excute step0 >> %log_path% :: 输入所需备份多久的数据,主要备份腔室表和 transfer表 :step1 echo %time% begin excute step1 >> %log_path% set /p param_day=请输入需要备份数据的天数(最大为120): echo. echo %param_day%|findstr "^[0-9]*$">nul if %errorlevel% NEQ 0 ( echo 请输入整数 echo. goto:step1 ) if %param_day% GTR 120 ( echo 备份时间超过 120 ,请重新输入 echo. goto:step1 ) set param_step=2 call:fun_save_param echo %time% end excute step1 >> %log_path% :: 备份客户端 ibdata :step2 echo %time% begin excute step2 >> %log_path% set tryCount=0 :backup set /a tryCount=%tryCount% + 1 if %tryCount% EQU 4 ( echo !!!!!备份 %param_ibdataPath% 失败,请手动备份至其他磁盘后,再继续执行脚本!!!!! echo. pause set param_step=3 call:fun_save_param exit 0 ) echo %time% begin backup ibdata1 echo %time% begin backup ibdata1 >> %log_path% copy /Y/V/Z "%param_ibdataPath%" %param_volume%\%dataSavePath%\ibdata1 if %errorlevel% NEQ 0 goto:backup echo %time% end backup ibdata1 echo. set param_step=3 call:fun_save_param echo %time% end backup ibdata1 >> %log_path% echo %time% end excute step2 >> %log_path% :: 清理数据库中备份表,及备份表中数据 :step3 echo %time% begin excute step3 >> %log_path% echo %time% begin clean history table for /f "skip=1" %%a in ('mysql -h%mysqlServer% -u%mysqlUser% -p%mysqlPassword% -e "select count(0) from information_schema.tables where table_name='backuphistory' and table_schema='%database_scope%'"') do ( if %%a EQU 1 ( goto:history ) else ( goto:skiphistory ) ) :history for /f "skip=1 tokens=1" %%i in ('mysql -h%mysqlServer% -u%mysqlUser% -p%mysqlPassword% --database %database_scope% -e "select backupname from backuphistory where backuptime < date_sub(curdate(), interval %param_day% day)"') do ( echo %time% delete history table %%i echo %time% delete history table %%i >> %log_path% mysql -h%mysqlServer% -u%mysqlUser% -p%mysqlPassword% --database %database_scope% -e "delete from backuphistory where backupname='%%i'" 2>>%log_path% mysql -h%mysqlServer% -u%mysqlUser% -p%mysqlPassword% --database %database_scope% -e "drop table %%i" 2>>%log_path% ) :skiphistory echo %time% end clean history table echo. set param_step=4 call:fun_save_param echo %time% end excute step3 >> %log_path% :: 导出 scope 相关的表 :step4 echo %time% begin excute step4 >> %log_path% :: 循环导出 obj 数组中的表 set currentIndex=0 :exportStartLoop if %currentIndex% EQU %tableCounts% goto:exportEndLoop for /f "usebackq delims==. tokens=1-3" %%i in (`set obj[%currentIndex%]`) do ( set curObj.%%j=%%k ) set export_ingnore_table=%export_ingnore_table% --ignore-table=%database_scope%.%curObj.table% if %currentIndex% LSS %param_tableIndex% ( set /a currentIndex=%currentIndex% + 1 goto:exportStartLoop ) echo %time% begin export table %curObj.table% echo %time% begin export table %curObj.table% >> %log_path% set tryCount=0 :: 导出失败进行 3 次尝试 :exporttable if %tryCount% EQU 3 ( echo 导出表 %curObj.table% 失败 无法继续 请联系软件工程人员 echo. pause exit 0 ) if %param_day% EQU 0 ( mysqldump -h%mysqlServer% -u%mysqlUser% -p%mysqlPassword% --no-data --databases %database_scope% --tables %curObj.table% > %param_volume%\%dataSavePath%\%curObj.table%.sql 2>>%log_path% ) else ( mysqldump -h%mysqlServer% -u%mysqlUser% -p%mysqlPassword% --databases %database_scope% --tables %curObj.table% --where="%curObj.field%>date_sub(curdate(), interval %param_day% day)" > %param_volume%\%dataSavePath%\%curObj.table%.sql 2>>%log_path% ) if %errorlevel% NEQ 0 ( echo %time% failed to export table:%curObj.table% >> %log_path% set /a tryCount=%tryCount% + 1 goto:exporttable ) echo %time% end export table %curObj.table% echo. echo %time% end export table %curObj.table% >> %log_path% set /a currentIndex=%currentIndex% + 1 set /a param_tableIndex=%currentIndex% call:fun_save_param goto:exportStartLoop :exportEndLoop :: 导出 scope 库 set tryCount=0 :exportscope if %tryCount% EQU 3 ( echo 导出库 %database_scope% 失败 无法继续 请联系软件工程人员 echo. pause exit 0 ) if %database_scope%% NEQ null ( echo %time% begin export database %database_scope% echo %time% begin export database %database_scope% : %export_ingnore_table% >> %log_path% mysqldump -h%mysqlServer% -u%mysqlUser% -p%mysqlPassword% --databases --events --routines %export_ingnore_table%> %param_volume%\%dataSavePath%\%database_scope%.sql 2>>%log_path% echo %time% end export database %database_scope% echo. echo %time% end export database %database_scope% >> %log_path% ) if %errorlevel% NEQ 0 ( echo %time% failed to export database:%database_scope% >> %log_path% set /a tryCount=%tryCount% + 1 goto:exportscope ) set param_step=5 call:fun_save_param echo %time% end excute step4 >> %log_path% :: 导出fa300 库 :step5 echo %time% begin excute step5 >> %log_path% set tryCount=0 :exportfa300 if %tryCount% EQU 3 ( echo 导出库 %database_fa300% 失败 无法继续 请联系软件工程人员 echo. pause exit 0 ) if %database_fa300% NEQ null ( echo %time% begin export database %database_fa300% echo %time% begin export database %database_fa300% >> %log_path% mysqldump -h%mysqlServer% -u%mysqlUser% -p%mysqlPassword% --databases --events --routines %database_fa300% > %param_volume%\%dataSavePath%\%database_fa300%.sql 2>>%log_path% echo %time% end export database %database_fa300% echo. echo %time% end export database %database_fa300% >> %log_path% ) if %errorlevel% NEQ 0 ( echo %time% failed to export database:%database_fa300% >> %log_path% set /a tryCount=%tryCount% + 1 goto:exportfa300 ) set param_step=6 set param_tableIndex=0 call:fun_save_param echo ********** export data successfully *********** echo. echo %time% exprot data successfully >> %log_path% echo %time% end excute step5 >> %log_path% :: 将干净的 ibdata 替换至客户端 :step6 echo %time% begin excute step6 >> %log_path% echo %time% begin stop mysql echo %time% begin stop mysql >> %log_path% call:fun_stopmysql echo %time% end stop mysql echo. set tryCount=0 :replace :: 三次失败尝试,如果都失败,就需要收到操作 set /a tryCount=%tryCount% + 1 if %tryCount% EQU 4 ( echo !!!!!替换 %param_ibdataPath% 失败!!!!! echo. echo 请手动将脚本目录下 ibdata1 文件替换至 %param_ibdataPath% echo. pause set param_step=7 call:fun_save_param exit 0 ) ::替换 ibdata1 文件 echo %time% begin replace ibdata1 echo %time% begin replace ibdata1 >> %log_path% copy /Y/V/Z ibdata1 "%param_ibdataPath%" if %errorlevel% NEQ 0 goto:replace echo %time% end replace ibdata1 echo. echo %time% end replace ibdata1 >> %log_path% set param_step=7 call:fun_save_param echo ********** replace ibdate1 successfully *********** echo. echo %time% end excute step6 >> %log_path% :step7 echo %time% begin excute step7 >> %log_path% echo %time% begin start mysql echo %time% begin start mysql >> %log_path% call:fun_startmysql echo %time% end start mysql echo. echo %time% end start mysql >> %log_path% :: 导入fa300库 set tryCount=0 :importfa300 if %tryCount% EQU 3 ( echo 导入库 %database_fa300% 失败 无法继续 请联系软件工程人员 echo. pause exit 0 ) if %database_fa300% NEQ null ( echo %time% begin import basedata %database_fa300% echo %time% begin import basedata %database_fa300% >> %log_path% mysql -h %mysqlServer% -u %mysqlUser% -p%mysqlPassword% < %param_volume%\%dataSavePath%\%database_fa300%.sql 2>>%log_path% echo %time% end import basedata %database_fa300% echo. echo %time% end import basedata %database_fa300% >> %log_path% ) if %errorlevel% NEQ 0 ( echo %time% failed to import database:%database_fa300% >> %log_path% set /a tryCount=%tryCount% + 1 goto:importfa300 ) set param_step=8 call:fun_save_param echo %time% end excute step7 >> %log_path% :step8 echo %time% begin excute step8 >> %log_path% call:fun_startmysql :: 导入 scope 库 set tryCount=0 :importscope if %tryCount% EQU 3 ( echo 导入库 %database_scope% 失败 无法继续 请联系软件工程人员 echo. pause exit 0 ) if %database_scope% NEQ null ( echo %time% begin import basedata %database_scope% echo %time% begin import basedata %database_scope% >> %log_path% mysql -h %mysqlServer% -u %mysqlUser% -p%mysqlPassword% < %param_volume%\%dataSavePath%\%database_scope%.sql 2>>%log_path% echo %time% end import basedata %database_scope% echo. echo %time% end import basedata %database_scope% >> %log_path% ) if %errorlevel% NEQ 0 ( echo %time% failed to import database:%database_scope% >> %log_path% set /a tryCount=%tryCount% + 1 goto:importscope ) set param_step=9 call:fun_save_param echo %time% end excute step8 >> %log_path% :step9 echo %time% begin excute step9 >> %log_path% call:fun_startmysql :: 循环导入数组中的表 :importStartLoop if %param_tableIndex% EQU %tableCounts% goto:importEndLoop for /f "usebackq delims==. tokens=1-3" %%i in (`set obj[%param_tableIndex%]`) do ( set curObj.%%j=%%k ) echo %time% begin import table %curObj.table% echo echo %time% begin import table %curObj.table% >> %log_path% set tryCount=0 :importtable if %tryCount% EQU 3 ( echo 导入表 %curObj.table% 错误无法继续,请联系软件工程人员处理 echo. pause exit 0 ) mysql -h %mysqlServer% -u %mysqlUser% -p%mysqlPassword% %database_scope% < %param_volume%\%dataSavePath%\%curObj.table%.sql 2>>%log_path% if %errorlevel% NEQ 0 ( echo %time% failed to import table:%curObj.table% >> %log_path% set /a tryCount=%tryCount%+1 goto:importtable ) echo %time% end import table %curObj.table% echo. echo %time% end import table %curObj.table% >> %log_path% set /a param_tableIndex=%param_tableIndex% + 1 call:fun_save_param goto:importStartLoop :importEndLoop set param_step=10 call:fun_save_param echo %time% end excute step9 >> %log_path% echo ********* imported data successfully *********** echo. :: 校验表是否导入 :step10 echo %time% begin check results echo %time% begin check results >> %log_path% set currentIndex=0 :checkStartLoop if %currentIndex% EQU %tableCounts% goto:checkEndLoop for /f "usebackq delims==. tokens=1-3" %%i in (`set obj[%currentIndex%]`) do ( set curObj.%%j=%%k ) mysql -h %mysqlServer% -u %mysqlUser% -p%mysqlPassword% %database_scope% -e "select %curObj.field% from %curObj.table% limit 1" >>%log_path% 2>&1 if %errorlevel% NEQ 0 ( echo %curObj.table% 表校验失败,请联系软件工程师处理!!! echo. pause exit 0 ) set /a currentIndex=%currentIndex% + 1 goto:checkStartLoop :checkEndLoop echo %time% end check results echo %time% end check results >> %log_path% echo ******** program executed successfully ******** echo. rename %param_volume%\%dataSavePath% %database_scope%_%var_date% >> %log_path% 2>&1 del /Q/F %process_param% >> %log_path% 2>&1 :: 计算任务执行时间 set end_time=%time% echo end time %end_time% call:fun_time_diff %begin_time%,%end_time% echo total time %excute_h%:%excute_m%:%excute_s% pause :fun_time_diff set param1=%1 set param2=%2 set /a h1=%param1:~0,2% 2>nul set /a m1=1%param1:~3,2% - 100 2>nul set /a s1=1%param1:~6,2% - 100 2>nul set /a h2=%param2:~0,2% 2>nul set /a m2=1%param2:~3,2% - 100 2>nul set /a s2=1%param2:~6,2% - 100 2>nul if %h2% LSS %h1% set /a h2=%h2%+24 set /a ts1=%h1%*3600 + %m1%*60 + %s1% set /a ts2=%h2%*3600 + %m2%*60 + %s2% set /a ts=%ts2% - %ts1% set /a excute_h=%ts%/3600 set /a excute_m=(%ts%-%excute_h%*3600)/60 set /a excute_s=%ts%%%%(60) goto:EOF :fun_save_param echo param_date=%date:~0,10%>%process_param% echo step=%param_step% >>%process_param% echo day=%param_day% >>%process_param% echo volume=%param_volume%>>%process_param% echo ibdataPath=%param_ibdataPath%>>%process_param% echo tableIndex=%param_tableIndex% >>%process_param% goto:EOF :fun_stopmysql tasklist|find /i "mysqld.exe" >> %log_path% 2>&1 if %errorlevel% NEQ 0 goto:EOF if %handwork% EQU 0 goto:stopmysql echo !!!!!! 请手动停止mysql 服务,然后继续 !!!!!! echo. pause echo. goto:fun_stopmysql ::使用命令停止 mysql 服务 :stopmysql net stop mysql >> %log_path% 2>&1 if %errorlevel% NEQ 0 set handwork=1 goto:fun_stopmysql goto:EOF :fun_startmysql tasklist|find /i "mysqld.exe" >> %log_path% 2>&1 if %errorlevel% EQU 0 goto:EOF if %handwork% EQU 0 goto:startmysql echo !!!!!!请手动启动 mysql 服务,然后继续!!!!!! echo. pause echo. goto:fun_startmysql :startmysql net start mysql >> %log_path% 2>&1 if %errorlevel% NEQ 0 set handwork=1 goto:fun_startmysql goto:EOF
最新发布
06-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值