MySQL, other knowledge points supplement

                                  1. View

1. What is a view?

A MySQL view (View) is a virtual table. Like a real table, a view is also composed of columns and rows, but the view does not actually exist in the database . The row and column data comes from the tables used in the query that defines the view, and is also dynamically generated when the view is used.

The database only stores the definition of the view, and does not store the data in the view. These data are all stored in the real table referenced by the definition view query. When using a view to query data, the database will retrieve the corresponding data from the real table. Therefore, the data in the view is dependent on the data in the real table. Once the data in the real table changes, the data displayed in the view will also change .

The view can select information that is useful to the user from the original table, and information that is useless to the user or that the user does not have permission to understand can be directly blocked, which is similar to filtering. Doing so not only simplifies the application, but also ensures the security of the system.

2.  The difference between view and data table

  • The view is not a real table in the database, but a virtual table whose structure and data are based on the query of the real table in the data.

  • The query operation SQL statement stored in the database defines the content of the view. The column data and row data come from the actual table referenced by the view query, and these data are dynamically generated when the view is referenced .

  • Views have no actual physical records and are not stored in the database in the form of datasets. The corresponding data is actually stored in the real table referenced by the view.

  • Views are windows to data, and tables are content. The table is the storage unit of the actual data, and the view only displays the data in different ways, and its data source is still the actual table .

  • A view is a way to view a data table. It can query the data composed of certain fields in the data table. It is just a collection of some SQL statements. From a security point of view, the data security of the view is higher, and the user who uses the view does not touch the data table and does not know the table structure.

  • The establishment and deletion of a view only affects the view itself, not the corresponding basic table.

3. View advantages 

1) Customize user data and focus on specific data

In the actual application process, different users may have different requirements for different data.

For example, when databases exist at the same time, such as student basic information tables, curriculum tables, and teacher information tables, etc., different users can use their own data according to their needs. Students view the view to modify their basic information, arrange course personnel to view the view to modify the curriculum and teacher information, and teachers view the view of student information and course information table.

2 Simplify data manipulation

When using queries, aggregation functions are often used, and information about other fields must be displayed at the same time. It may also need to be associated with other tables. The statement may be very long. If this action occurs frequently, you can create a view to simplify the operation .

3 Improve data security

Views are virtual and do not physically exist. To protect the security of the underlying data, you can only grant user view permissions without specifying the permissions to use tables.

4 Share required data

By using views, each user does not have to define and store the data they need, but can share the data in the database, and the same data only needs to be stored once.

5 Change data format

By using views, retrieved data can be reformatted and organized for output to other applications.

6) Reuse SQL statements

The view provides the encapsulation of the query operation and does not contain data itself. The presented data is retrieved from the base table according to the view definition. If the data in the base table is added or deleted, the view also presents the updated data. Once a view is defined, it can be easily reused after writing the required queries.

4. Usage

4.1 Grammar:

CREATE VIEW <视图名> AS <SELECT语句>

4.2 Grammar Description 

The syntax is explained below.

  • <视图名>: Specifies the name of the view. The name must be unique within the database and cannot have the same name as another table or view.

  • <SELECT语句>: Specifies the SELECT statement to create a view, which can be used to query multiple underlying tables or source views.

The following restrictions apply to the specification of a SELECT statement in a create view:

  • In addition to the CREATE VIEW permission, the user also has the relevant permissions on the underlying tables and other views involved in the operation.

  • SELECT statements cannot refer to system or user variables.

  • The SELECT statement cannot contain subqueries in the FROM clause.

  • A SELECT statement cannot refer to prepared statement parameters.


                                           2. Index 

1. What is an index?

In MySQL, an index is a data structure used to improve query efficiency . It is a structure created on one or more columns in a database table to speed up data retrieval and query operations.

MySQL supports many types of indexes, including:

1. B-tree index: Most MySQL indexes are implemented based on the B-tree data structure. It is suitable for range queries, sorting and equality queries, and can quickly locate data on larger data sets.
2. Unique index: used to ensure that the value of the index column is unique, usually used to constrain the uniqueness in the table.
3. Primary key index: It is a special unique index used to uniquely identify each row of data in the table. Primary key indexes are usually created automatically.
4. Full-text index: used in full-text search scenarios, which can quickly search and match text columns.
5. Hash index: It is suitable for scenarios where only equivalent query is performed. Hash index can quickly locate data, but does not support range query.

Creating an index can be done on one or more columns in a table with the `CREATE INDEX` statement. At the same time, for common query requirements, MySQL will automatically create some implicit indexes, such as primary key indexes and unique indexes.

The benefit of indexes is that they speed up queries because the database system does not need to scan the entire table for specific data. Instead, it can use the structure of the index to locate the required data more quickly. However, indexes also require additional storage space and incur performance overhead for insert, update, and delete operations. Therefore, it is necessary to balance query performance and data update requirements when using indexes.

When designing a database, common situations where indexes need to be created include:

- Primary key field: A field used to uniquely identify each row of data.
- Foreign key fields: fields used to relate other tables.
- Frequently queried fields: Fields that often appear in query conditions.
- Fields that are often used for sorting and grouping.

It should be noted that too many or unnecessary indexes may lead to performance degradation and waste of storage space. Therefore, you need to carefully consider the actual query requirements and the frequency of data updates when creating indexes.

To sum up, an index in MySQL is a data structure used to improve query efficiency, which speeds up query operations by quickly locating and accessing data. Proper use of indexes can significantly improve database performance.

2. Why use an index?

The main purpose of using indexes is to improve the query performance and response speed of the database . Here are some important reasons to use indexes:

1. Speed ​​up data retrieval : The index allows the database engine to directly locate the data rows that meet the query conditions without scanning the entire table. This can significantly reduce the time and resources required for queries, especially when querying large amounts of data in large database tables.

2. Improve query performance : By quickly locating data, indexes can greatly reduce query execution time and improve query performance. Especially on frequently executed queries, the use of indexes can significantly speed up the return of query results.

3. Reduce disk I/O operations : The index organizes the data according to a specific structure and stores it on the disk. By using indexes, the database can minimize the number of data blocks that need to be read, thereby reducing the number of disk I/O operations.

4. Support sorting and grouping operations : Indexes can provide optimization for common sorting and grouping operations. By using the index, the database engine can quickly locate and process the data according to the specified sort order or grouping field, reducing the time consumption of sorting and grouping operations.

5. Improve concurrency performance : In a concurrent database environment, multiple queries executed at the same time may cause performance problems. Using appropriate indexes can reduce locking and contention and improve concurrency performance, so that multiple queries can complete in a shorter period of time.

It should be noted that the creation and maintenance of indexes consume certain computing and storage resources, and additional performance overhead may be introduced when inserting, updating, and deleting operations on tables. Therefore, when designing indexes, it is necessary to balance query performance and data update frequency, and avoid excessive or unnecessary indexes.

To sum up: using indexes can speed up database queries, improve query performance, reduce disk I/O operations, and support sorting and grouping operations. Proper use of indexes can significantly improve the efficiency and responsiveness of the database.
 

3. Advantages and disadvantages

Indexes have their obvious advantages, as well as their inevitable disadvantages.

advantage

The advantages of indexes are as follows:

  • The uniqueness of each row of data in the database table can be guaranteed by creating a unique index.

  • Indexes can be set on all MySQL column types.

  • It can greatly speed up the query speed of data, which is the main reason for using indexes.

  • The connection between tables can be accelerated in realizing the referential integrity of data.

  • It can also significantly reduce the time of grouping and sorting in the query when using the grouping and sorting clauses for data query

shortcoming

Adding indexes also has many disadvantages, mainly as follows:

  • Creating and maintaining index groups is time-consuming and increases with the amount of data.

  • Indexes need to occupy disk space. In addition to data tables occupying data space, each index also occupies a certain amount of physical space. If there are a large number of indexes, index files may reach their maximum file size sooner than data files.

  • When the data in the table is added, deleted, and modified, the index must also be dynamically maintained, which reduces the speed of data maintenance.

Indexes can improve query speed, but will affect the speed of inserting records. Because, when inserting records into a table with an index, the database system will sort according to the index, which reduces the speed of inserting records, and the speed impact when inserting a large number of records will be more obvious. In this case, the best way is to delete the index in the table first, then insert the data, and then create the index after the insertion is complete.

4. When is an index not suitable?

  • table records too few

  • Tables that are frequently added, deleted, or modified

  • Table fields with repeated and evenly distributed data should only be indexed for frequently queried and most frequently sorted data columns (if a certain data class contains too much repeated data, indexing does not make much sense)

  • Frequently updated fields are not suitable for creating indexes (will increase the IO burden)

  • The fields that are not used in the where condition do not create indexes

5. When does the index fail

  • like starts with the wildcard % and the index is invalid

  • When the full table scan is faster than the index query, the full table scan will be used instead of the index

  • The index will be invalid if the string is not added with single quotes

  • The index column in where uses a function (such as a substring string interception function)

  • The index column in where has operations (the index on the right will be invalid if you use < or >, and the index will not be invalid if you use <= or >=)

  • is null can use the index, is not null cannot use the index (depending on the specific situation of a column)

  • The composite index does not use the left column field (the leftmost prefix rule, if the leftmost column index is not used, or a column with an index is skipped in the middle, the index will be partially invalidated)

  • If there is or in the condition, the previous column has an index, but the latter column does not, and the index will be invalid. If you want the index to take effect, you can only add an index to each column in the or condition 

6. Index classification

MySQL indexes are divided into ordinary indexes, unique indexes, primary key indexes, composite indexes, and full-text indexes. The index will not contain columns with null values. Index items can be null (unique index, composite index, etc.), but as long as there are null values ​​in the column, they will not be included in the index.

6.1 Common Index

Ordinary index is the most basic index, it does not have any restrictions;

- **创建索引语法:**

```
 create index index_name on table(column);
```

- **修改表结构方式添加索引:**

```
ALTER TABLE table_name ADD INDEX index_name ON (column(length))
```

- **删除索引**

```
DROP INDEX index_name ON table
```

6.2. Unique index

A unique index is similar to the previous ordinary index, the difference is that the value of the index column must be unique, but null values ​​are allowed. If it is a composite index, the combination of column values ​​must be unique.

CREATE UNIQUE INDEX indexName ON table(column(length))

 6.3. Primary key index

The primary key index is a special unique index. A table can only have one primary key, and null values ​​are not allowed. To put it simply: the primary key index is to speed up the query + the column value is unique (null is not allowed) + there is only one in the table.

CREATE TABLE mytable( 
    ID INT NOT NULL, 
    username VARCHAR(16) NOT NULL, 
    PRIMARY KEY(ID) 
);

6.4. Composite indexes

A composite index refers to an index created on multiple fields, and the index will be used only if the first field when the index is created is used in the query condition. Follow the leftmost prefix set when using composite indexes.

ALTER TABLE `table` ADD INDEX name_city_age (name,city,age);

6.5. Full-text indexing

  • concept

The full-text index is mainly used to find keywords in the text, rather than directly comparing with the values ​​in the index. The fulltext index is very different from other indexes. It is more like a search engine than a simple parameter matching of where statement.

The fulltext index is used with the match against operation instead of the general where statement plus like. It can be used in create table, alter table, and create index, but currently only char, varchar, and text columns can create full-text indexes.

It is worth mentioning that when the amount of data is large, putting the data into a table without a global index and then using CREATE index to create a fulltext index is much faster than first creating a fulltext for a table and then writing the data.

  • version support

Before Mysql 5.6, only MyISAM supports full-text indexing. After 5.6, both Innodb and MyISAM support full-text indexing. In addition, the full-text index can only be built if the data type of the field is char, varchar, text and its series.

  • the case

Create data tablet_articles

CREATE TABLE t_articles (
    id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
    title VARCHAR(200),
    body TEXT,
    FULLTEXT (title,body)
);
给现有的article表的title和body字段创建全文索引,索引名称为fulltext_article

ALTER TABLE t_articles ADD FULLTEXT INDEX fulltext_article (title, body);
导入测试数据

INSERT INTO t_articles VALUES
(NULL,'MySQL Tutorial', 'DBMS stands for DataBase ...'),
(NULL,'How To Use MySQL Efficiently', 'After you went through a ...'),
(NULL,'Optimising MySQL','In this tutorial we will show ...'),
(NULL,'1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
(NULL,'MySQL vs. YourSQL', 'In the following database comparison ...'),
(NULL,'MySQL Security', 'When configured properly, MySQL ...');
INSERT INTO t_articles VALUES
(NULL,'abc', 'zs red blue ...'),
(NULL,'xyz', 'ls white');
INSERT INTO t_articles VALUES
(NULL,'aaa', 'zs red blue ...'),
(NULL,'bbb', 'ls white red');\

  • Example 1: Natural language mode (IN NATURAL LANGUAGE MODE, default mode)

SELECT * FROM t_articles where MATCH (title,body) AGAINST ('database')
  • Example 2: Boolean search mode (IN BOOLEAN MODE)

symbol meaning
+ must contain this string
- must not contain this string
"" Words cannot be split as a whole within double quotation marks
> Improve the relevance of the word, and the results of the query are at the top
< Reduce the relevance of the word, and the results of the query are lower
* Wildcards, which can only be followed by words
包含red或者blue
SELECT * FROM t_articles where MATCH (title,body) AGAINST ('red blue' IN BOOLEAN MODE );
#包含red,但是必须包含blue
SELECT * FROM t_articles where MATCH (title,body) AGAINST ('red +blue' IN BOOLEAN MODE );
#包含red,但是一定不能包含blue
SELECT * FROM t_articles where MATCH (title,body) AGAINST ('red -blue' IN BOOLEAN MODE );

Note: Only English keywords with a length of more than 2 will take effect

  • Example 3: Chinese search

After 5.6, MySQL has its own ngram parser, which can parse Chinese, Japanese, and Korean characters. If the ngram parser is not used, MySQL uses spaces and symbols as separators by default (it is enough for English, but it is not easy to use for Chinese, Japanese, and Korean characters, so the ngram parser is needed).

Check database version

select version();

View database engine

show engines;

Modify the minimum allowed characters for MySQL full-text search

[mysqld]
ft_min_word_len = 2

The minimum allowable characters for full-text search (default 4, you can view it through SHOW VARIABLES LIKE 'ft_min_word_len'), Chinese is usually two characters is one word, so it is best to modify this value to 2 for Chinese. services.msc

Note: Mysql service must be restarted

Create a full-text index and set up an ngram parser

ALTER TABLE t_book
ADD FULLTEXT INDEX fulltext_bookname_type (bookname, booktype) WITH PARSER ngram;

Create a full-text index based on the bookname and booktype fields in the t_book table and set the ngram parser to support Chinese retrieval.

Keyword search:

SELECT * FROM t_book where MATCH (bookname, booktype) AGAINST ('三国')

                               3. Database backup and recovery

1. Data import and export

- DELETE/TRUNCATE

-- Time spent: 10.597s

DELETE FROM t_log;

-- Time spent: 0.121s

TRUNCATE TABLE t_log;

-- 1. Use tools like Sqlyog, Navicat, etc. to import and export data.

-- Using the Navicat tool to import t_log takes a total of 45s;

-- Using mysqldump to import the entire database containing the t_log table takes a total of 20s;

-- It takes 7.502s to import load data infile using single table data

2. Use mysqldump to import and export

2.1 Export

 2.1.1 Export table data and table structure

mysqldump -u用户名 -p密码 数据库名 > 数据库名.sql(这个名字随便叫)

    D:\SoftwareInstallPath\mysql-8.0.13-winx64\bin>mysqldump -uroot -p123456 mybatis_ssm > 1234567.sql

2.1.2 Export only the table structure

mysqldump -u用户名 -p密码 -d 数据库名 > 数据库名.sql

  mysqldump -uroot -p -d abc > abc.sql

    注:导出的数据在mysql的bin目录下

  2.2 import

    注意:首先建立空数据库

    mysql>create database abc;

    2.2.1 Method 1

 

   mysql>use abc;                   #选择数据库

    mysql>set names utf8;            #设置数据库编码

    mysql>source /D:/SoftwareInstallPath/mysql-8.0.13-winx64/bin/1234567.sql;  #导入数据

    2.2.2 Method 2

 

  mysql -u用户名 -p密码 数据库名 < 数据库名.sql

      #mysql -uabc_f -p abc < abc.sql

  可先通过SELECT INTO OUTFILE方式,将数据导出到Mysql的C:\ProgramData\MySQL\MySQL Server 5.5\data目录下,再通过LOAD DATA INFILE方式导入。



  1) select * from 表名 into outfile '/文件名.sql';

  2) load data infile '/文件名.sql' into table 表名(列名1,...);

 这时候就可以在 mysql.ini 文件的 [mysqld] 代码下增加 secure_file_priv=E:/TEST 再重启 mysql 就可以了。然后在导出的地址下面写上刚才配置的这个地址 eg: select * from tb_test into outfile "E:/TEST/test.txt";就可以了。

select * from t_log into outfile 'E:/12345678.sql';

load data infile 'E:/12345678.sql' into table t_log(id,ip,userid,moduleid,content,createdate,url);

show variables like 'secure%'

desc t_log

select * FROM t_log;


 

Guess you like

Origin blog.csdn.net/lz17267861157/article/details/131615444