How GaussDB creates and manages views

How GaussDB creates and manages views

1. What is a view?

When the user is interested in a combination of certain fields in one or more tables in the database, but does not want to type these queries every time, the user can define a view to solve this problem.

A view is different from a basic table in that it does not actually exist physically and is a virtual table. The database only stores the definition of the view, not the data corresponding to the view. These data are still stored in the original basic table. If the data in the basic table changes, the data queried from the view will also change. In this sense, a view is like a window through which the data and changes in the database that the user is interested in can be seen. The view is run every time it is referenced.

2. Construct test data

Step 1 Execute the following statement to create a test table.

CREATE TABLE infotest (  id int,  name varchar(30) ,  price float ,  PRIMARY KEY (id)) ;

Step 2 Execute the following statement to insert data into the table.

insert into infotest values ​​(1001,'Toothbrush',10.5),(1002,'Towel',21.5),(1003,'Coffee Table',999.9 ),(1004,'TV',3199),(1005,'refrigerator',2999),(1006,'1 mobile phone',1999) ,(1007,'2 mobile phone',7699),(1008,'.3 mobile phone',699.9),(1009,'T-shirt', 21.5),(1010,'Jeans',99);

3. Create a view

Execute the following statement to create a new view MyView, in which infotest is the table created in constructing test data.

CREATE OR REPLACE VIEW MyView AS SELECT * FROM infotest WHERE price < 1000;

CREATE VIEW

OR REPLACE in CREATE VIEW is optional. When OR REPLACE exists, it means that the view will be replaced if it previously existed.

For more usage of CREATE VIEW, please refer toCreate View.

4. Query view

Execute the following statement to query the MyView view.

SELECT * FROM MyView;

  id  |   name    | price 
------+-----------+------- 
 1001 | 牙刷    |  10.5 
 1002 | 毛巾    |  21.5 
 1003 | 茶几    | 999.9 
 1008 | .3 手机 | 699.9 
 1009 | T恤      |  21.5 
 1010 | 牛仔裤 |    99 
(6 rows)

5. View specific information of a view

Execute the following statement to query the detailed information of MyView.

\d+ MyView

                         View "root.myview" 
 Column |         Type          | Modifiers | Storage  | Description 
--------+-----------------------+-----------+----------+------------- 
 id     | integer               |           | plain    | 
 name   | character varying(30) |           | extended | 
 price  | double precision      |           | plain    | 
View definition: 
 SELECT  * 
   FROM infotest 
  WHERE infotest.price < 1000::double precision;

6. Update data

Step 1 Execute the following statement to update data.

UPDATE infotest SET name = '手机' where id = 1008;

Step 2 After updating the data, query the updated information through the view.

SELECT * FROM MyView;

  id  |   name    | price 
------+-----------+------- 
 1001 | 牙刷    |  10.5 
 1002 | 毛巾    |  21.5 
 1003 | 茶几    | 999.9 
 1009 | T恤      |  21.5 
 1010 | 牛仔裤 |    99 
 1008 | 手机    | 699.9 
(6 rows) 

7. Change the view name

Step 1 Execute the following statement to rename the view.

ALTER VIEW  MyView RENAME TO YourView;

ALTER VIEW

For more usage, please refer toChange View.

Step 2 Execute the following command to check the effect of the name change.

\d+ YourView

                        View "root.yourview"
 Column |         Type          | Modifiers | Storage  | Description
--------+-----------------------+-----------+----------+-------------
 id     | integer               |           | plain    |
 name   | character varying(30) |           | extended |
 price  | double precision      |           | plain    |
View definition:
 SELECT  *
   FROM infotest
  WHERE infotest.price < 1000::double precision;

8. Delete view

Execute the following command to delete the MyView view.

drop view YourView;

DROP VIEW

How GaussDB creates and manages scheduled tasks 

When users are using the database, if they perform some long-term tasks during the day (such as statistical data summary or data synchronization tasks from other databases), it will have a performance impact on normal business, so users often choose to Executed at night, which increases the user's workload. Therefore, the database GaussDB provides the function of scheduled tasks. Users can create scheduled tasks. When the task time point is reached, the execution of the task can be automatically triggered, thereby reducing the user's operation and maintenance workload.

GaussDB provides the creation of scheduled tasks, automatic execution of tasks upon expiration, task deletion, and modification of task attributes (including: task id, task closing and opening, task triggering time, triggering interval, task content, etc.).

 

1. Scheduled task management

Step 1 Create a test table:

postgres=# CREATE TABLE test(id int, time date);

When the result is displayed as the following information, the creation is successful.

CREATE TABLE

Step 2 Create a custom stored procedure:

postgres=# CREATE OR REPLACE PROCEDURE PRC_JOB_1()
AS
N_NUM integer :=1;
BEGIN
FOR I IN 1..1000 LOOP
INSERT INTO test VALUES(I,SYSDATE);
END LOOP;
END;
/

When the result is displayed as the following information, the creation is successful.

CREATE PROCEDURE

Step 3 Create the task:

  • The newly created task (without job_id specified) means that the stored procedure PRC_JOB_1 is executed every 1 minute.
postgres=# call dbe_task.submit('call public.prc_job_1(); ', sysdate, 'interval ''1 minute''', :a);
job
-----
1
(1 row)
  • Specify job_id to create a task, where the available range of job_id is 1~32767.
postgres=# call dbe_task.id_submit(2,'call public.prc_job_1(); ', sysdate, 'interval ''1 minute''');
isubmit
---------
 
(1 row)

Step 4 View the task information created by the current user through the view.

postgres=# select job,dbname,start_date,last_date,this_date,next_date,broken,status,interval,failures,what from my_jobs;
job | dbname |     start_date      |         last_date          |         this_date          |      next_date      | broken | status |      interval       | failures |           what
-----+--------+---------------------+----------------------------+----------------------------+---------------------+--------+--------+---------------------+----------+---------------------------
1 | postgres   | 2017-07-18 11:38:03 | 2017-07-18 13:53:03.607838 | 2017-07-18 13:53:03.607838 | 2017-07-18 13:54:03 | n      | s      | interval '1 minute' |        0 | call public.prc_job_1();
(1 row)

Step 5 Stop the task.

postgres=# call dbe_task.finish(1,true);
broken
--------
 
(1 row)

Step 6 Start the task.

postgres=# call dbe_task.finish(1,false);
broken
--------
 
(1 row)

Step 7 Modify task properties:

  • Modify the Next_date parameter information of JOB.

--Modify the Next_date of Job1 to start execution one hour later.

postgres=# call dbe_task.next_time(1, sysdate+1.0/24);
next_date
-----------
 
(1 row)
  • Modify the Interval parameter information of the JOB.

--Modify the Interval of Job1 to be executed every 1 hour.

postgres=# call dbe_task.interval(1,'sysdate + 1.0/24');
interval
----------
 
(1 row)
  • Modify the What parameter information of the JOB.

--Modify the What of Job1 to execute the SQL statement "insert into public.test values(333, sysdate+5);".

postgres=# call dbe_task.content(1,'insert into public.test values(333, sysdate+5);');
what
------
 
(1 row)
  • Modify JOB's Next_date, Interval, What and other parameter information at the same time.
postgres=# call dbe_task.update(1, 'call public.prc_job_1();', sysdate, 'interval ''1 minute''');
change
--------
 
(1 row)

Step 8 Delete JOB.

postgres=# call dbe_task.cancel(1);
remove
--------
 
(1 row)

 Summarize

GaussDB is a distributed relational database independently developed and developed by Huawei. This product supports distributed transactions, deployment across AZs in the same city, zero data loss, 1000+ scalability, and PB-level mass storage. At the same time, it has key capabilities such as high availability, high reliability, high security, elastic scaling, one-click deployment, fast backup and recovery, and monitoring and alarming on the cloud. It can provide enterprises with an enterprise-level database with comprehensive functions, stability, reliability, strong scalability, and superior performance. Serve.

The overall architecture of GaussDB distributed form is as follows:

おすすめ

転載: blog.csdn.net/GaussDB/article/details/135011403