TL;DR
This paper introduces Coco, a novel tool that automatically discovers data constraints within the source code of database-backed web applications. By leveraging these discovered constraints, Coco optimizes application performance through data layout changes, query rewrites, and adding pre-checks, significantly improving query execution times.
What Problem Does This Paper Address?
Modern web applications heavily rely on databases to store and manage data. While traditional query optimization techniques leverage database-level constraints (like primary keys or foreign keys), these often miss a wealth of implicit data relationships embedded within the application's code itself. Developers often express these relationships through validation logic or class associations, which are not directly understood by database optimizers. Existing methods for discovering data constraints from data statistics alone are insufficient for programmatically generated datasets common in web applications. This gap means that valuable optimization opportunities are left unrealized, leading to suboptimal application performance.
Methods
Coco employs a two-phase approach:
- Offline Optimization:
- Constraint Detection: Coco statically analyzes the application's source code (specifically Ruby on Rails applications in their implementation) by parsing Abstract Syntax Trees (ASTs) and pattern matching. It identifies various constraint types, including inclusion, presence, length, uniqueness, format, numerical, and foreign key constraints, by looking at built-in ORM validation APIs and custom validation functions.
- Query Extraction: Coco analyzes application test logs to extract query templates and their parameters.
- Optimization Strategy Generation: Based on the discovered constraints, Coco generates potential optimizations. These include:
- Data Layout Optimization: Changing data types (e.g., from string to enum) for columns with inclusion constraints to save space and speed up queries.
- Pre-checks: Adding checks to application logic to avoid issuing queries if user inputs violate known constraints (e.g., format or length).
- Query Rewriting: This is the most complex part. Coco uses an "enumerate-test-verify" approach:
- Enumeration: It generates candidate query rewrites based on identified constraints (e.g., removing
DISTINCTif a column is unique). - Cost Estimation & Testing: It estimates the cost of each candidate rewrite using the database optimizer and filters out slower ones. It also uses a test database to check for semantic equivalence.
- Formal Verification: A formal verifier (based on Cosette's U-semiring semantics) is used to guarantee semantic equivalence between the original and rewritten queries.
- Lookup Table Generation: The verified, optimized query templates are stored in a lookup table.
- Online Query Replacement: When the application runs, Coco intercepts outgoing queries. If a query's template is found in the lookup table, Coco replaces it with the optimized version before sending it to the database.
Datasets
The paper evaluates Coco on 14 real-world open-source web applications written in Ruby on Rails. These applications span categories like forums, collaboration, e-commerce, social networks, and map applications. For evaluation, they also generated synthetic datasets ranging from 5-10GB with 10K to 1M records per table.
Key Insights and Ideas
- Hidden Constraints: A significant number of valuable data constraints are implicitly defined in application code (e.g., validation logic) rather than explicitly in the database schema. These are often missed by traditional optimization techniques.
- Code Analysis is Key: Analyzing application source code, rather than just database statistics, is crucial for discovering these hidden constraints.
- "Enumerate-Test-Verify" for Rewrites: Directly applying database-level rewrite rules is often insufficient or too complex. Coco's approach of generating, testing, and formally verifying rewrites is a robust way to leverage application-specific constraints.
- Database Optimizers' Limitations: Mainstream database optimizers rely on pattern matching and heuristics, which are often too general and fail to utilize application-specific constraints effectively.
- Holistic Optimization: Coco demonstrates that optimizing not just queries but also data layout and application logic (via pre-checks) can lead to substantial performance gains.
Key Results
- Coco discovered an average of 289 constraints per application across the 14 evaluated real-world web applications.
- A small overlap (12%) exists between constraints defined in application code and those explicitly declared in the database, highlighting Coco's ability to find latent constraints.
- For the 6 evaluated applications, Coco optimized 13.8% of queries through data layout changes and 47% through application code changes.
- Overall, Coco improved the performance of 2511 queries, with 118 queries achieving over 2x speedup.
- Changing string columns with inclusion constraints to enum types reduced storage by an average of 2.19x and improved query performance by 1.05x.
- Adding pre-checks for invalid inputs yielded an average speedup of 5x when 80% of inputs were invalid.
Limitations and Future Work
- Analyzability: Coco assumes application code is analyzable. It cannot guarantee constraint validity if third-party libraries with unavailable source code modify fields involved in constraints.
- Custom Validation Complexity: While Coco handles custom validation functions, it relies on pattern matching and may miss constraints defined in highly complex or unconventional custom code.
- Data Integrity: Coco's checker program can help identify data integrity issues when external changes modify the database, but ensuring data integrity remains a developer responsibility.
- Future Work: The authors suggest that their approach is agnostic to specific ORMs and databases, implying potential for broader application. They also note that the "enumerate-test-verify" approach can be extended with new rewrite rules. The accuracy of cost estimation for rewrites could be further improved.
Summary updated