views and indexes
A view is a saved question An index is a saved shortcut Neither is a copy of the data in the way people assume
view
CREATE VIEW high_targets AS
SELECT host, port FROM targets WHERE severity = 'critical';
SELECT host FROM high_targets;
Output:
beta.local
The view reflects the table at read time It does not freeze results
index
CREATE INDEX idx_targets_port ON targets(port);
Queries filtering on port can use this
Writes pay a cost to maintain it
Add indexes because a query is slow and the plan shows a scan, not because a column exists
Choosing
- view when the same query is used by several consumers
- index when reads dominate and the filter is predictable
- neither when the table has a few hundred rows and the scan is faster
Next
Not helping an attacker in 09 safety
Posted