简体   繁体   中英

Mysql create SQL subquery ALIAS

Basically, I have multiple queries like this:

SELECT a, b, c FROM (LONG QUERY) X WHERE ...

The thing is, I am using this LONG QUERY really frequently. I am looking to give this subquery an alias which would primarily:

  1. Shorten & simplify queries (also reduce errors and code duplication)
  2. Possibly optimize performance. (I believe this is done by default by mysql query caching)

Till now, I have been doing it this way to store:

variable = LONG QUERY;
Query("SELECT a, b, c FROM ("+variable+") X WHERE ...");

Which is not bad. I am looking for a way to do this with mysql internally.

Is it possible to create a simple, read-only view that would generate NO overhead, so I could do everywhere? I believe this is more propper & readable way of doing it.

SELECT a, b, c FROM myTable WHERE ...

Typically these are called views. For example:

CREATE VIEW vMyLongQuery  
AS 
    SELECT a, b, c FROM (LONG QUERY) X WHERE ...

Which can then be referenced like this:

SELECT a, b, c FROM vMyLongQuery 

See http://dev.mysql.com/doc/refman/5.0/en/create-view.html for more info on syntax.

As far as performance goes, best case performance will be near enough exactly the same as what you're doing now and worst case it will kill your app. It depends on what you do with the views and how MySQL processes them.

MySQL implements views two ways merge and temptable . The merge option is pretty much exactly what you're doing now, your view is merged into your query as a subquery. With a temptable it will actually spool all the data into a temptable and then select/join to that temptable. You also lose index benefits when data is joined to the temptable.

As a heads up, the merge query plan doesn't support any of the following in your view.

  • Aggregate functions (SUM(), MIN(), MAX(), COUNT(), and so forth)
  • DISTINCT
  • GROUP BY
  • HAVING
  • LIMIT
  • UNION or UNION ALL
  • Subquery in the select list
  • Reference to literals without an underlying table

So if your subquery uses these, you're likely going to hurt performance.

Also, heed OMG Ponies' advice carefully, a view is NOT the same as a base class. Views have their place in a DB but can easily be misused. When an engineer comes to the database from an OO background, views seem like a convenient way to promote inheritance and reusability of code. Often people eventually find themselves in a position where they have nested views joined to nested views of nested views. SQL processes nested views by essentially taking the definition of each individual view and expanding that into a beast of a query that will make your DBA cry.

Also, you followed excellent practice in your example and I encourage you to continue this. You specified all your columns individually, never ever use SELECT * to specify the results of your view. It will, eventually, ruin your day.

Im not sure if this is what you are looking for but you can use stored procedures to recall mysql queries. Im not sure if you can use it inside another query though?

http://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM