How can you implement Inline view in MariaDB?


Inline View in MariaDB is a powerful feature that allows you to use a subquery in the FROM clause, treating it as a virtual table. This approach simplifies complex queries, improves readability, and enables advanced data manipulation. However, inline views in MariaDB require explicit aliases and have specific algorithm options and limitations that developers must consider for optimal performance and maintainability. The key requirement is that every inline view must have an alias.

Basic Syntax

SELECT column_list 
FROM (SELECT ... FROM table) AS alias 
WHERE conditions;

Key Features

Mandatory Alias

Unlike Oracle, MariaDB requires explicit aliases for inline views. For example:

SELECT A.LAST_NAME, A.SALARY, A.DEPARTMENT_ID, B.SAL_AVG
FROM EMPLOYEES A,
    (SELECT DEPARTMENT_ID, ROUND(AVG(SALARY)) AS SAL_AVG
      FROM EMPLOYEES
      GROUP BY DEPARTMENT_ID) B
WHERE A.DEPARTMENT_ID = B.DEPARTMENT_ID;

Algorithm Options

MariaDB supports three view algorithms:

  • MERGE: Combines view definition with the main query
  • TEMPTABLE: Stores results in a temporary table
  • UNDEFINED: Lets MariaDB choose the optimal algorithm

Limitations

MERGE Algorithm Restrictions

Inline views cannot use MERGE if they contain:

  • HAVING clauses
  • GROUP BY statements
  • DISTINCT operations
  • Aggregate functions (MAX, MIN, SUM, COUNT)
  • Subqueries in the SELECT list
  • UNION operations

View Updates

For an inline view to be updatable, it must:

  • First, Contain all base table columns without default values
  • Then, Have simple columns without derived calculations
  • Last but not least, must not contain multiple references to base table columns

Conclusion

Using MariaDB Inline Views effectively can enhance query performance and simplify data retrieval. However, understanding their constraints—such as algorithm restrictions and update limitations—is crucial for avoiding inefficiencies. By leveraging the right view algorithm and structuring queries optimally, developers can maximize the benefits of inline views while maintaining database efficiency.

https://minervadb.xyz/optimizing-join-operations-in-mysql-8-for-enhanced-performance-and-scalability/
https://minervadb.xyz/knowledge-base/how-to-use-postgresql-dynamic-statistics-views-for-troubleshooting-performance/
https://minervadb.xyz/how-system-calls-are-implemented-in-linux-kernel/

 

 

About MinervaDB Corporation 60 Articles
A boutique private-label enterprise-class MySQL, MariaDB, MyRocks, PostgreSQL and ClickHouse consulting, 24*7 consultative support and remote DBA services company with core expertise in performance, scalability and high availability. Our consultants have several years of experience in architecting and building web-scale database infrastructure operations for internet properties from diversified verticals like CDN, Mobile Advertising Networks, E-Commerce, Social Media Applications, SaaS, Gaming and Digital Payment Solutions. Our globally distributed team working on multiple timezones guarantee 24*7 Consulting, Support and Remote DBA Services delivery for MySQL, MariaDB, MyRocks, PostgreSQL and ClickHouse.