database performacne Flashcards
What is database performance?
Database performance refers to the effectiveness of database systems in managing data operations, measured by response time, throughput, and resource utilization.
Why is database tuning important?
- Improves user experience
- Optimizes resource usage
- Ensures consistency and reliability
What is database design in the context of performance tuning?
Database design involves structuring the database to reduce redundancy, ensure data integrity, and enhance performance through optimal schema, table, column, relationship, and constraint management.
What are the consequences of bad database design?
- Redundancy
- Update anomalies
- Inefficiency
- Scalability issues
- Loss of flexibility
What is indexing?
Indexing is the process of creating a data structure to improve the speed of data retrieval operations on a database table at the cost of additional writes and storage space.
When should indexes be used?
- Columns frequently used in the WHERE clause
- Columns used in JOIN conditions
- Columns used for sorting data in ORDER BY clauses
What is query optimization?
Query optimization involves rewriting queries, choosing efficient execution paths, and using database features like indexes and partitioning to reduce the time and resources required to execute SQL queries.
What are the principles of query optimization?
- Understanding execution plans
- Proper use of indexes
- Query rewriting
- Keeping statistics and cardinality estimates up to date
- Leveraging database features like partitioning and materialized views
Provide an example of a simple optimization by filtering early.
Bad:
SELECT * FROM sales WHERE year = 2023;
Better:
SELECT customer_id, total FROM sales WHERE year = 2023;
What is the advantage of using SQL functions?
SQL functions encapsulate complex operations into reusable blocks of code, improving performance, ensuring consistency, and maintaining data integrity by centralizing logic within the database.
Provide an example of a SQL function to check order limits.
CREATE OR REPLACE FUNCTION check_order_limit()
RETURNS TRIGGER AS $$
DECLARE
total_orders_for_day NUMERIC;
BEGIN
SELECT SUM(amount) INTO total_orders_for_day
FROM orders
WHERE customer_id = NEW.customer_id AND
date_trunc(‘day’, order_date) = date_trunc(‘day’, NEW.order_date);
IF total_orders_for_day + NEW.amount > 5000 THEN RAISE EXCEPTION 'Total orders for the day exceed the £5,000 limit.'; END IF; RETURN NEW; END; \$\$ LANGUAGE plpgsql;
CREATE TRIGGER check_order_before_insert
BEFORE INSERT OR UPDATE ON orders
FOR EACH ROW EXECUTE FUNCTION check_order_limit();
What is the importance of monitoring and continuous improvement in database performance tuning?
Continuous monitoring helps identify performance bottlenecks, inefficient queries, and data integrity issues, allowing for proactive issue resolution and maintaining optimal database performance.
What key areas should be monitored for database performance?
- Query performance
- Resource utilization (CPU, memory, disk I/O)
- Index usage and efficiency
- Errors and warnings
What is the continuous improvement cycle for database performance tuning?
- Assess
- Plan
- Implement
- Review