Database design experience
Whether the design of the database model is reasonable will greatly affect the performance of the system. Based on many years of experience in designing and using databases, the author proposes the following design principles for colleagues' reference.
Use cursors with caution
Cursors provide a means of scanning line by line in a specific collection. Generally, cursors are used to traverse data line by line, and perform different operations based on different conditions for retrieving data. For cursor (large data collection) loops defined in multiple tables and large tables, it is easy for the program to enter a long wait or even crash. The author is doing the end-of-day account rollover interest calculation in the "Housing Provident Fund Management System" of a certain city. When processing a cursor with 100,000 accounts, the program entered an indefinite wait (later calculated to take 48 hours to complete) (hardware environment: Alpha/4000 128MB RAM, SCO Unix, Sybase 11.0). After modifying the program and using the UPDATE statement instead, the process was completed within 20 minutes. An example is as follows:
Declare Mycursor cursor for select count—no from COUNT
Open Mycursor
Fetch Mycursor into @vcount—no
While (@@sqlstatus=0)
Begin
If @vcount—no=′ ′ Condition 1
Operation 1
If @vcount—no=′ ′ Condition 2
Operation 2
...
Fetch Mycursor into @vcount—no
End
...
Change to
Update COUNT set operation 1 for condition 1
Update COUNT set operation 2 for condition 2
...
A cursor must be used in some cases In this case, you can consider transferring the data rows that meet the conditions into a temporary table, and then define a cursor on the temporary table to operate. In this way, the performance can be significantly improved. In the background program design of the "Telecom Charging System" database in a certain city, the author performed cursor operations on a table (more than 30 qualified rows of data out of 30,000 rows) (hardware environment: PC server, PⅡ266 64MB RAM, Windows NT4.0 MS SQL Server 6.5).
An example is as follows:
Create #tmp /* Define temporary table*/
(Field 1
Field 2
...)
Insert into #tmp select * from TOTAL where condition
Declare Mycursor cursor for select * from #tmp /*Define cursor for temporary table*/
...
Tips for using index (Index)
Creating an index generally has two purposes: maintaining the index Uniqueness of indexed columns and strategies to provide fast access to data in the table. There are two types of indexes in large databases, namely cluster index and non-cluster index. A table without cluster index stores data in a heap structure, and all data is added at the end of the table; while a table with a cluster index has its data physically stored in the database. will be stored in the order of cluster index keys. A table is only allowed to have one cluster index. Therefore, according to the B-tree structure, it can be understood that adding any kind of index can improve the speed of querying by index columns, but at the same time it will slow down the insertion speed. , the performance of update and delete operations, especially when the fill factor (Fill Factor) is large. Therefore, when frequently inserting, updating, and deleting operations are performed on a table with many indexes, a smaller fill factor should be set when creating the table and index, so as to leave more free space in each data page and reduce page segmentation and re- organization's work.