Basics of database test development interview

Database foundation

Affairs

A transaction refers to a group of database access operations that constitute a single logical processing unit. The SQL statements of these operations are encapsulated together, and they are either successfully executed or not executed, and each SQL statement is not executed immediately after execution Database, but submit all sql execution results after commit

Transaction characteristics

  • Atomicity
  • Consistency: One correct data state changes to another correct state
  • Isolation: concurrently executed transactions cannot interfere with each other, isolation level
  • Persistence: The changes to the data in the database after the transaction is committed are permanent

Consequences of transaction concurrency

  • Dirty read: read dirty data that was modified by another concurrent transaction but was not committed successfully
  • Non-repeatable read: After reading the data, the data is modified by another transaction, that is, the data read twice is different
  • Phantom Reading: Reading the data agreed to share, found that new data was added for the second time
  • Lost update: After updating the shared data, read it again and find that the updated value is different

Transaction isolation level

  • Read uncommitted: cause all the above conditions such as dirty reads
  • Read submitted: Dirty reads can be avoided , but there are other situations
  • Repeatable read: can avoid dirty read , non-repeatable read
  • Serializable: Dirty reads, non-repeatable reads, phantom reads, lost updates can be avoided .
    The later, the lower the concurrent performance

Transaction classification

  • Flat transaction: frequent and simple, the operations are all at the same level, either all are executed or not executed.
    Restrictions: cannot commit or roll back a certain part of the transaction, or commit in several steps
  • Flat transaction with savepoint: Allow rollback to an earlier state in the same transaction during transaction execution , set savepoint to save a certain state of the transaction.Limit
    : When the system crashes , all savepoints disappear, easy lost
  • Chain transaction: When committing a transaction, release unnecessary data objects, and implicitly pass the necessary processing context to the next transaction to be started. Commit the transaction operation and start the next transaction operation will be combined into one atomic operation. This means that the next thing will see the result of the previous transaction , as if it were in a transaction.
  • Nested transactions: hierarchical structure. It consists of a top-level transaction in control of affairs at all levels (top-level transaction). Below the top-level transaction nested transaction is called subtransactions (subtransaction), which controls each of the partial conversion.
  • Distributed transaction: usually a flat transaction running in a distributed environment , so it is necessary to access different nodes in the network according to the location of the data

Database optimization

Three paradigms

  • First normal form: attribute atomicity, indivisible
  • Second normal form: Eliminate some functional dependencies, non-primary attributes must depend on the primary attributes
  • The third paradigm: eliminate transitive dependencies, non-primary attributes must directly depend on the primary attributes, and transitive dependencies cannot be

Database integrity

  • Entity integrity: the primary key is unique, not empty
  • Referential integrity: The primary key and the foreign key are consistent, the foreign key can be empty, but it cannot be incorrect
  • User-defined integrity: constraints on a certain column of data

Sub-database and sub-table

  • Vertical Split: a vertical split is to take the press table module into different databases, the databases in the module and functions to split out table, tends to a Service
  • Horizontal splitting: Horizontal splitting is to divide a table into different tables or databases according to certain rules . For example, by time, account number rules, year, modulo algorithm, etc.

Disadvantages of sub-table

  • Difficulty in pagination query
  • Very limited query

Solution
Generally, the main table stores all the data, and then splits it horizontally according to the business , and the function of mycar sub-table.


Slow query

MySQL does not respond to SQL results within 10 seconds by default, then it is a slow query.
You can modify the default time of MySQL slow query

Modify slow query

--查询慢查询时间
show variables like 'long_query_time';
--修改慢查询时间
set long_query_time=1; ---但是重启mysql之后,long_query_time依然是my.ini中的值

The difference between index and primary key

key: a physical structure, has two meanings

  • Constraints: Take care of database integrity
  • Index: auxiliary query

Primary key

Binding effect: to regulate the primary key and a unique storage, while this is also the key to establish a primary key index , mysql automatically establish primary key Unique Index

  • The primary key uniquely identifies each record in the database table
  • The primary key must contain a unique value
  • Primary key column cannot be NULL
  • There should be one and only one primary key in each table

Unique key

Constraint function: standardize the uniqueness of data, and establish a unique index on it to uniquely identify a record

  • Both the primary key and the unique key provide uniqueness guarantees for the columns. The difference is that there can be multiple unique keys in a table, and there can only be one primary key
  • The key of mysql has two meanings: constraint and index

index

  • Index is the physical structure of the database, it only assists in querying. When it is created, it will be stored in another table space in a directory-like structure.
  • Index is divided into prefix index, full text index, etc.;
  • The index is only an index, and does not restrict the behavior of the indexed field (that is what the key does)
Index classification

Primary key index: must be specified as "PRIMARY KEY", there is no PRIMARY Index
unique index: unique index, generally written as unique key
ordinary index: index, only this kind is pure index


database backup

  • Full backup
    Commonly used, backup the entire database, including all database objects such as user tables, system tables, indexes, views, and stored procedures. But it also takes more time and space, so it is generally recommended to do a full backup once a week.
  • Transaction log backup The
    transaction log is a separate file that records database changes. When backing up, you only need to copy the changes made to the database since the last backup, so it only takes a little time. In order to make the database robust, it is recommended to back up the transaction log every hour or more frequently.
  • Differential backup is
    also called incremental backup . It is another way to back up only a part of the database. It does not use the transaction log. Instead, it uses a new image of the entire database. It is smaller than the initial full backup because it only contains the databases that have changed since the last full backup. Its advantage is fast storage and recovery speed. It is recommended to do a differential backup every day.
  • File backup A
    database can consist of many files on the hard disk. If the database is very large and cannot be backed up overnight, you can use file backup to back up part of the database every night. Because the database is generally not large enough to use multiple file storage, this kind of backup is not very common.

Database recovery

Database recovery technology is to use database backup files and database transaction log files to achieve database recovery processing. According to the user's recovery requirements, the database should be restored by rolling forward or rolling back transactions.

Roll forward
  • If you need to restore the database backup file before the failure , you can use the roll-forward transaction recovery method
  • Roll forward represents recovery , that is, unfinished transactions, let them continue to execute
  • Write from memory to disk

Incompletely committed transactions, that is, the transaction has been executed by the commit command, but now only part of the dirty data block corresponding to the transaction modification has been written to the data file on the disk, and some have been set as the commit mark The dirty block is still in the memory. If the database instance crashes at this time, when the database instance is restored, you need to use rollforward (this mechanism) to complete the complete commit of the transaction, that is, the previous part has been set to the commit mark and returned The dirty blocks in the memory are written to the data file on the disk

Rollback
  • If you need to restore the database backup file after the failure , you can use the rollback transaction recovery method.
  • Revoke
  • Part of the dirty data that has been written to the disk is undone

Uncommitted transaction, that is, the transaction has not been executed commit command. But at this time, part of the dirty blocks modified by the transaction may also be written into the data file. If the database instance crashes at this time, when the database instance is restored, rollback (this mechanism) is needed to undo the dirty blocks that have been written to the data file previously.


Frequently test SQL statements

Find the names of students with two scores> 80 in the student transcript

select s.name from student s
where s.score > 80
group by s.name
having count(*)>=2;

How mysql optimizes the backup of Mysql
database How to implement
mysql creates a student table, including id (int) and name (string), the creation of the primary key: CREATE TABLE stu(id INT (5), name VARCHAR (100), PRIMARY KEY (id));
mysql create index CREATE INDEX index_name ON table_name (column_list) CREATE INDEX idx_c4 ON t(c4);
database query 10-20 line content: select * from stu limit 10, 10;

Find the phone starting with 135: select * from table where tel like '135%';
left join, right join and inner join affect the performance factors

Guess you like

Origin blog.csdn.net/qq_38879305/article/details/107189341