Mysql performance tuning (2)

Preface

  Starting from the last article , we began to introduce to you the optimization part of mysql. The last article first introduced you to the installation and configuration of mysql in Linux, and then introduced you to the relevant content of the index, including: an overview of the index, the advantages and disadvantages of the index, and also introduced you to the index The underlying principle of storage engine and index is the difference and connection between B+ tree and B tree. Finally, I will introduce you to the classification, creation, modification, and viewing and deletion of indexes. The final introduction is some of the main principles we are creating indexes.
  The following article introduces you to the knowledge of views, mainly including the basic concepts of views, creating or modifying views to the final view. Another introduction is the related content of triggers, including related concepts of triggers, creating log tables, inserting, modifying, and deleting triggers.
  Next, I will introduce the content related to the view.

One, view

1. View overview

  The view is actually a kind of virtual table. The view does not actually exist in the database. The row and column data is derived from the table used in the query that defines the view, and is dynamically generated when the view is used. In layman's terms, a view is the result set returned after a select statement is executed. So when we create the view, the main job is to create this SQL query statement.
  Compared with ordinary tables, views have the following advantages:

  • Simple : The user who uses the view does not need to care about the structure of the corresponding table, the associated conditions and the filter conditions. For the user, it is the result set of the filtered compound conditions.
  • Security : Users who use views can only access the result sets that they are allowed to query. The authority management of the table cannot be restricted to a certain row or column, but it can be easily implemented through the view.
  • Data independence : Once the structure of the view is determined, the impact of table structure changes on users can be shielded. Adding columns in the source table has no effect on the view; modifying the column names of the source table can be solved by modifying the view, which will not cause visitor damage influences.

2. Create or modify views

  Next, we will introduce you to create and modify views. The first thing we will introduce to you is to create views. The syntax for creating a view is as follows:

create [or replace] [algorithm = {
   
   undefined | merge | temptable}]
view view_name [(column_list)]
as select_statement
[with [cascaded | local] check option]

  Next, I will introduce you to modify the view. The syntax for modifying the view is as follows:

alter [algorithm = {
   
   undefined | merge | temptable}]
view view_name [(column_list)]
as select_statement
[with [cascaded | local] check option]

  What needs attention here is to introduce you to the corresponding related options in the grammar;

  • with [cascaded | local] check option : Determines whether to allow data to be updated so that the record no longer meets the conditions of the view.
  • local : It can be updated as long as the conditions of this view are met.
  • cascaded : All the conditions for all views of this view must be met before they can be updated.

  We use a case to create and modify the view. The specific table and data are the data of the previous article . The specific sql statement is as follows:

create or replace view city_country_view
as
select t.*, c.country_name from country c, city t where c.country_id = t.country.id;

3. View the view

  I have introduced you to modify and create views before, and show you how to create and modify views in practice. Next, I will introduce you to the related content of the view view.
  In fact, starting from the mysql5.1 version, when the show tables command is used, not only the name of the table is displayed, but the name of the view is also displayed. There is no show views command that displays the view separately . The specific commands are as follows:

show tables;


  Also when using the show table status command, not only the table information can be displayed, but also the view information can be realized.

show table status

4. Delete the view

  Finally, I will introduce you to the deleted view. The specific syntax is as follows:

drop view [if exists] view_name [, view_name]...[restrict | cascade];

  I introduced you to an example of deleting a view. The view city_country_view we created before deletes the view; the specific content is as follows:

drop view city_country_view;

Second, the trigger

  I have introduced you to the related content of the view, including the overview of the view and the operations related to the creation, modification, viewing and deletion of the view. Next, I will introduce you to the related content of triggers, including the basic concepts of triggers and creating triggers. Delete triggers, view triggers and other related content. First, I will introduce the basic content of triggers.

1. Basic concepts of triggers

  Trigger is a database object related to the table, which refers to the set of SQL statements defined in the trigger and executed before or after insert/update/delete. This feature of triggers can assist applications in ensuring data integrity, logging, data verification and other operations on the database side. Use aliases OLD and NEW to refer to the changed record content in the trigger, which is similar to other databases. Current triggers only support row-level triggers, not statement-level triggers. The specific trigger types are used as follows:

2. Create a trigger

  Next, I will introduce you to the syntax structure of creating triggers:

create trigger trigger_name
before/after insert/update/delete
on tb1_name [for each row]
trigger_stmt;

  After introducing the grammar of creating triggers, let's give you a case to create the corresponding triggers. The requirements of this case are as follows:

  The data change log of the emp table is recorded through triggers, including addition, modification, and deletion.

  Therefore, we first create a log table

create table emp_logs(
	id int(11) not null auto_increment,
	operation varchar(20) not null commit '操作类型, insert/update/delete',
	operate_time datetime not null comment "操作时间",
	operate_id int(11) not null comment "操作表的ID",
	operate_params varchar(500) comment "操作参数",
	primary key(`id`)
)engine=innodb default charset=utf8;

  We create a trigger on the created table, and record the data change log emp_logs of the emp table through the trigger, including addition, modification, and deletion. The specific statements are as follows:

create trigger emp_insert_trigger
after insert on emp
for each row
begin
	insert into emp_logs(id, operation, operate_time, operate_id, operate_params)
	values(null, 'insert', now(), new.id, concat('插入后(id:', new.id, ',name', new.name,', age:', new.age, ', salary: ', new.salary, ')'));
end$

create trigger emp_update_trigger
after update on emp
for each row
begin
	insert into emp_logs(id, operation, operate_time, operate_id, operate_params)
	values(null, 'update', now(), new.id, concat('修改前(id:', old.id, ',name', old.name,', age:', old.age, ', salary: ', old.salary, ')', '修改后(', new.id, ',name', new.name,', age:', new.age, ', salary: ', new.salary, ')'));
end$

create trigger emp_update_trigger
after delete on emp
for each row
begin
	insert into emp_logs(id, operation, operate_time, operate_id, operate_params)
	values(null, 'delete', now(), old.id, concat('删除前(id:', old.id, ',name', old.name,', age:', old.age, ', salary: ', old.salary, ')'));
end$

3. Delete the trigger

  I introduced the creation and modification of triggers. Next, I will introduce the syntax structure of creating triggers:

drop trigger [schema_name.]trigger_name

  In fact, if schema_name is not specified, the default is the current database.

4. View the trigger

  Previously introduced the creation, deletion, and modification of triggers, and then look at triggers. We can view the status, syntax and other information of the trigger by executing the show triggers command.

show trigger;

to sum up

  Starting from the last article , I will introduce the optimization of mysql performance. This article introduces the related content of views and triggers. Views mainly include the basic concepts of views, creating and modifying views, as well as viewing and deleting views. In addition, I will introduce you to triggers, which mainly introduce the basic concepts of triggers and the various operations of trigger creation, deletion, and viewing of triggers. Therefore, mysql is a very important skill. Almost every job in the computer needs a mysq skill. Therefore, we need special mastery. Life is endless and struggle is endless. We work hard every day, study hard, constantly improve our abilities, and believe that we will learn something. Come on! ! !

Guess you like

Origin blog.csdn.net/Oliverfly1/article/details/109273428