Database Concepts related issues

Database concepts related issues

8.  how to optimize database (Oracle data in the database of millions of records)?

Use Index

Create the partition, partition index

Using stored procedures

9.  How to optimize SQL statements

    ①.  Optimization operator try not to use Index without using operators

    如:in ,not in , is null, is not null,<>等

    ②.  Some optimization of field conditions

     Treatment functions using field can not use the index,

     Be explicit or implicit computation of the field can not be indexed

     The conditions can not be indexed field comprises a plurality of operation when this table

    ③.  Conditions in which the rear service SQL WHERE intensive impact of order

    ④.  Application of ORACLE HINT (tips) processing

    ⑤.  Affect the order of the look-up table 

10. The  use of index query will be able to improve the performance of queries it? why?

Can not. If a large number of rows returned using a full table scan better performance.

11. The  operator optimization

. IN operator

Written with the IN SQL advantage is relatively easy to write clear and easy to understand, which is more suitable for modern software development style. However, the SQL performance with IN is always low, steps from Oracle to analyze the SQL with IN and not the SQL IN has the following differences:

ORACLE trying to convert it into a plurality of connection tables, if unsuccessful, to perform the conversion IN subquery inside, then the outer query table records, if the conversion is successful direct connection with a plurality of query tables. This shows the SQL with IN at least a plurality of the conversion process. SQL in general can be converted successfully, but contains terms for SQL group statistics can not be converted.

Recommendation: intensive business which try not to use SQL IN operator, instead of using EXISTS scheme.

. NOT IN operator

This action is strongly not recommended, because it can not apply the index table.

Recommended programs: instead of using NOT EXISTS program

. IS NULL or IS NOT NULL operation (determine whether the field is blank)

Determine whether the field is empty index is generally not applied, because the index is not an index value of null.

Recommended Program: other operations instead of using the same functions, such as: a is not null to a> 0 or a> '' and so on. Allowed field is empty, instead of using a default null value, such as the application does not allow the status field is empty, the default application.

. > And <operators (greater than or less than the operator)

Greater or less than the operator under normal circumstances is not adjusted, the index will be used because it has to find the index, but in some cases it may be optimized, such as a table has one million records, a numeric field A, 30 million record a = 0,30 Wan record a = 1,39 Wan record of a = 2,1 Wan record a = 3. Then the implementation of A> 2 and A> = 3 there is a big difference, because A> 2 Shi ORACLE will first identify the records of the index is then compared, and A> = 3 Shi ORACLE directly to find = 3 records index.

. LIKE operator

LIKE operator can be applied to wildcard queries, wildcard combinations which may reach almost any query, but if used well will produce performance problems, such as LIKE '% 5400%' This query does not reference index, and LIKE 'X5400%' will index reference range.

A practical example: a business number that follows YW_YHJBQK table query user identification number may be open No. YY_BH LIKE '% 5400%' this condition will have a full table scan, if the change YY_BH LIKE 'X5400%' OR YY_BH LIKE 'B5400% 'index YY_BH the query will use the two ranges, the performance certainly greatly improved.

. UNION operator

UNION making table will filter out duplicate links records, so the result set in the table will link the resulting sort operation to remove duplicate records and then returns the result. Most practical applications are no duplicate records, the most common is the process table and the history table UNION. Such as:

select * from gc_dfys

union

select * from ls_jg_dfys

The SQL at runtime to remove the results of the two tables, and then sort space to sort remove duplicate records, and returns the result set, if a large amount of data, then the table may cause sorted by disk.

Recommended Program: The alternative operator UNION ALL UNION, UNION ALL operation as simply merge the two results after return.

select * from gc_dfys

union all

select * from ls_jg_dfys

12.  Tell me what you know some of the programs on query optimization?

1. query optimization, should try to avoid full table scan, should first consider indexing by the column involved in where and order.

2. Should be avoided fields null value judgment in the where clause, will cause the engine to give up using the index and full table scan, such as:?

     select id from t where num is null?

     You can set the default value of 0 on the num, num column in the table to ensure that there is no null value, then this query:?

     select id from t where num=0

3 should be avoided in the where clause! = Or <> operator, otherwise the engine to give up using the index and a full table scan.

4. should avoid or use to connect to the conditions in the where clause, will cause the engine to give up using the index and full table scan, such as:?

     select id from t where num=10 or num=20?

     Such inquiries can:?

     select id from t where num=10?

     union all?

     select id from t where num=20

5.in  and should not in caution, otherwise it will lead to a full table scan, such as:?

     select id from t where num in(1,2,3)?

     For continuous values, you can not use in between: the?

     select id from t where num between 1 and 3

6. The following query will also lead to a full table scan:?

     select id from t where name like '%abc%'?

     To improve efficiency, can be considered full-text search.

7. If the parameters in the where clause, will lead to a full table scan. Because SQL at runtime will only resolve local variables, but the optimizer can not defer the choice of access plan to run; it must choose at compile time. If, however, establish access plan at compile time, the value of the variable is unknown, and therefore can not be used as an index entry selected. As the following statement will perform full table scan:?

     select id from t where num=@num?

     It can be changed to force the query using the index:?

     select id from t with(index(索引名)) where num=@num

8 should be avoided to the fields in the where clause expression operations, which will cause the engine to give up using the index and a full table scan. Such as:?

     select id from t where num/2=100?

     Should read:?

     select id from t where num=100*2

9 should be avoided to a function operation in the fields where clause that will cause the engine to give up using the index and full table scan. Such as:?

     select id from t where substring (name , 1,3) = 'abc' - name beginning with the abc id?

     select id from t where datediff(day,createdate,'2005-11-30')=0--‘2005-11-30’生成的id?

     Should read:?

     select id from t where name like 'abc%'?

     select id from t where createdate>='2005-11-30' and createdate<'2005-12-1'

10. Do not carry out functions, arithmetic operations, or other expressions in the where clause "=" left, or the system may not work properly indexed.

11. In the field as a condition of using the index, if the index is a composite index, you must use the index to the first field to ensure the system uses the index as a condition, otherwise the index will not be used, and should as much as possible so that field order is consistent with the order index.

12. Do not write the query does not make sense, such as the need to create an empty table structure:?

     select col1,col2 into #t from t where 1=0?

     This code does not return any result sets, but consumes system resources and should be replaced by this:?

     create table #t(...)

13. In many cases instead of in use exists is a good choice:?

     select num from a where num in(select num from b)?

     Replace with the following statement:?

     select num from a where exists(select 1 from b where num=a.num)

14. Not all indexes are valid query, SQL query optimization is performed according to data in the table when the index column has a lot of duplication of data, SQL queries may not go to use the index, such as a table has a field sex, male, almost half of each female, even if the index built on sex also no effect on the query efficiency.

15. The index is not possible, the corresponding index can certainly improve the efficiency of select, but also reduces the efficiency of insert and update, because it is possible when the insert or update will rebuild the index, the index needs to be carefully considered how to build, As the case may be. An index number table is best not more than six, if too much you should consider some of the less frequently used to build the index column if necessary.

16 should be avoided as much as possible clustered index update data columns, because the order of the column index data is clustered physical storage order recorded in the table, once the column will result in the adjustment value changes in the order of recording the entire table, will consume considerable resources. If applications require frequent updates clustered index data columns, you need to consider whether it should be built for the clustered index index.

17 make use of numeric fields, if only the fields containing numerical information is not possible for the character design, which reduces the performance of the connections and queries, and increases storage costs. This is because the engine when processing queries and connections one by comparing each character in the string, and for numeric comparison purposes only once is enough.

18. Use as a varchar / nvarchar instead of char / nchar, because first of all variable-length fields small storage space, you can save storage space, followed by the query, in a relatively small field of search efficiency is clearly higher.

19. anywhere Do not use select * from t, with a specific list of fields instead of "*", do not return any of the fields with less than.

20 make use of a temporary table instead of the variable table. If the table variable contains a large amount of data, please note that the index is very limited (only the primary key index).

21. Avoid frequent create and delete temporary tables, system tables to reduce the consumption of resources.

22. A temporary table is not unusable, they can make appropriate use of certain routines more efficient, for example, when it is necessary a large table or tables commonly duplicate references a data set. However, for a one-time event, it is best to use export table.

23. When the new temporary table, if one inserts a large amount of data, it may be used instead of select into create table, to avoid a large number of log, in order to increase speed; if small data, in order to ease the system resource table, should create table, then insert.

24. If you use a temporary table to be sure all the temporary table explicit deleted at the end of the stored procedure, first truncate table, then drop table, to avoid locking the system tables a long time.

25. Try to avoid using a cursor, because the poor efficiency of the cursor, if the cursor operation more than 10,000 lines, you should consider rewriting.

26. Use the cursor before the method or methods based on temporary tables, you should look for set-based solutions to solve the problem, usually more efficient set-based method.

27. temporary tables, cursors are not unusable. Use FAST_FORWARD cursor on small data sets are usually better than other progressive treatment methods, particularly in reference to several tables must be in order to obtain the required data. In the result set includes "total" than usual routines performed by using the cursor speed fast. If the development time permits, cursor-based methods and can be set based approach to try to see which method is better.

28. Set SET NOCOUNT ON at the beginning of all the stored procedures and triggers, SET NOCOUNT OFF disposed at the end. DONE_IN_PROC not need to send a message to the client after each statement is executed and triggers stored procedure.

29. Try to avoid large transaction operations, improve system concurrency.

30. Avoid returned to the client a large amount of data, if the data is too large, you should consider the corresponding demand is reasonable.

13.  talk about some of the views you optimize the performance of SQL query massive data?

Database system is the core of management information systems, online transaction processing database (OLTP) and online analytical processing (OLAP) is based on one of the most important computer application banks, corporations, government and other sectors. Examples of most systems from the application point of view, the proportion of various database query operation in the operation occupied the largest, and query operations are based on a SELECT statement in the SQL statement is the most costly of the statement. For example, if the amount of data accumulated to a certain degree, such as a bank account database table information accumulated to millions or even tens of millions of records, a full table scan often requires tens of minutes, or even hours. If the scan is better than a full table query strategy can often be reduced to a few minutes so that the query time, we can see the importance of query optimization techniques.

One million data query optimization techniques thirty see Question 12

14. The  database associated with outreach difference?

Internal coupling (inner join) a coupling, a common return columns of the table two matching lines

Coupling an external (outer join) a coupling, the coupling further comprising a coupling and those recorded in the table recording irrelevant. You can create an outer join three deformation does not match the line to specify included:

Left outer join, right outer joins, and full outer join.

left outer join (left outer join) the left of the table is the primary table, all columns; right table to take no null

right outer join (right outer join) to the right of the table is the primary table, all columns; the left column of the table row match only, no value is set to null

completely external coupling (full outer join) all columns, no value is set to null

15.  Are you familiar with the database what?

the Oracle L : Oracle;

l  DB2:IBM;

SQL Server L : Microsoft;

the Sybase L : Saier Si;

the MySQL L : Oracle;

16  respectively to talk about DDL, DML, DCL, DQL, respectively, refers to what?

The DDL (the Data Definition Language): a data definition language for defining database objects: databases, tables, columns and the like;

   Create a database: CREATE DATABASE [IF NOT EXISTS] mydb1;

   Delete the database: DROP DATABASE [IF EXISTS] mydb1;

   Modify the database code: ALTER DATABASE mydb1 CHARACTER SET utf8

The DML (the Data Manipulation Language): a data manipulation language, used to define the database records (data);

   Insert data: INSERT INTO table name (name column 1, column name 2, ...) the VALUES (value 1, value 2)

   Data Review: UPDATE SET column name table value 1 = 1, ... n = column name value n [WHERE conditions]

   To delete data: DELETE FROM table [WHERE conditions]

DCL (the Data Control Language): Data Control Language, used to define access rights and security levels;

   Create a user: CREATE USER username @ address IDENTIFIED BY 'password';

   Authorized Users: GRANT privileges 1, ..., n ON authority database * TO user name.

   Revoke the authorization: REVOKE privilege 1, ..., n ON authority database * FORM user name.

   View permissions: SHOW GRANTS FOR username

   Delete User: DROP USER username

   Change Password: USE mysql;

            UPDATE USER SET PASSWORD = PASSWORD ( ' password') WHERE User = 'username' and Host = 'IP';

            FLUSH PRIVILEGES;

DQL (the Data Query Language): data query language used to query records (data).

        SELECT selection_list / * name of the column to be queried * /

   FROM table_list / * table name to query * /

   WHERE condition / * line condition * /

   GROUP BY grouping_columns / * results * Packet /

   HAVING condition / * grouped row condition * /

   ORDER BY sorting_columns / * results packet * /

   LIMIT offset_start, row_count / * Results defined * /

17. The  database, which requires us to have a primary key?

    Non-null, unique, can be referenced!

18.  respectively, to talk about MySQL and oracle paging?

oracle use rownum plus the complete paging function nested subqueries

SELECT * FROM

(

SELECT A.*, ROWNUM RN

FROM (SELECT * FROM TABLE_NAME) A

WHERE ROWNUM <= 40

)

WHERE RN >= 21

MySQL using the limit function

SELECT * FROM table LIMIT 5,10; // query Article 6 to Article 15 data, note that 10-bit offset.

Guess you like

Origin www.cnblogs.com/coder-wf/p/12217008.html