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.