Database Learning - "Using Live SQL"

This morning, the teacher introduced some standard formats of simple statements in class, and introduced other knowledge such as SQL statement efficiency comparison, Oracle database environment , operating system and network optimization around the theme of "using live SQL" .

At the beginning, the teacher used the combination of "SELECT DISTINCT" and "SELECT DISTINCT FROM table name" and SELECT COUNT to query the number of "state-owned", "three capitals", "collective" and "private" enterprises in the legal person table as an example, let us see The difference in the execution efficiency of two similar statements. Analysis of its content shows that SELECT DISTINCT has performed more than one query in the table (for example, 4 queries will be performed in this question), while SELECT DISTINCT FROM table name has only been queried once. So the latter faster.

 

SELECT DISTINCT
	(SELECT COUNT(*) FROM LegalEntityT WHERE ENATURE='国营' ),
	(SELECT COUNT(*) FROM LegalEntityT WHERE ENATURE='三资' ),
	(SELECT COUNT(*) FROM LegalEntityT WHERE ENATURE='集体' ),
	(SELECT COUNT(*) FROM LegalEntityT WHERE ENATURE='私营' )
(FROM LegalEntityT)

 

 

Subqueries can appear in WHERE, FROM and other clauses. The standard format of these query statements is fixed and will not be repeated here. The teacher specifically mentioned that the result of the subquery exists in a temporary table generated in memory; in the execution efficiency of IN and EXISTS, we found that when there is enough content in the table, we can find that EXISTS is more efficient than IN. high, otherwise there is not much difference between the two. I searched on the Internet, and the explanation of the principle is: IN is a hash connection between the outer table and the inner table, while EXISTS is a loop for the outer table, and the inner table is queried each time the loop is looped. And it can be seen in the demo that the same form, only using IN or EXISTS, the execution efficiency is different, because it will be affected by other factors in the system. 

Select *
From LegalEntityT
Where Eno
    in (Select Eno From loant where lamount>1000)

 

Select *
From LegalEntityT
Where Exists(
    Select Eno From loant where loant.Eno= LegalEntityT.Eno
        and lamount>1000)

 

In the magic of case expressions, the teacher asked us to compare two different expressions that can achieve the same purpose case when then end:

 

select count(case feature when 'state-run' then 'state-run' end) as 'state-run',
count(case feature when 'private' then 'private' end) as 'private',
count(case enature when 'collective' then 'collective' end) as 'collective',
count(case enature when 'three capitals' then 'three capitals' end) as 'three capitals'
from dbo.LegalEntity

 

select count(case feature when 'state-run' then '1' end) as 'state-run',
count(case feature when 'private' then '2' end) as 'private',
count(case feature when 'collective' then '3' end) as 'collective',
count(case enature when 'three capitals' then '4' end) as 'three capitals'
from dbo.LegalEntity

  

The second code is relatively rare, and the numbers are replaced by other conditional expressions. If more than one CASE WHEN expression is eligible, only the first eligible clause will be returned, and the rest will be ignored.

 

In the part related to database design, according to what we have learned before, we are familiar with conceptual design and relational design, but are relatively unfamiliar with the physical design related to the underlying implementation of the database. It is determined by the storage structure , access method, access path, storage location and system configuration of the database .

 

Finally, I posted an error solution to open SQL Sever 2014 Management Studio today:

The error is as follows:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or could not be reached. Please verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open connection to SQL Server)"

Solution reference: http://www.jb51.net/article/59352.htm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326199577&siteId=291194637