Why our UI search was taking forever
Users kept complaining about search performance. With customers managing 1000+ VMs, they were constantly searching:
- VM by UUID: β550e8400-e29b-41d4-a716-446655440000β
- VM by name: βprod-database-01β
- VM by IP: β10.24.3.156β
- Volume by UUID: β7b3f6d2a-9c8e-4f5a-b1d2-3e4f5a6b7c8dβ
- Power host by hostname: βpower-host-dal-03β
- Hypervisor hosts, network IDsβ¦
Every. Single. Search. Was. A. Collection. Scan.
// What we were doing
db.vms.find({ $or: [
{ uuid: searchTerm },
{ name: searchTerm },
{ ip: searchTerm },
{ hostname: searchTerm }
]})
// Scanning 50k+ documents every time someone typed in the search box
The fix was actually simple:
// Individual indexes for each searchable field
db.vms.createIndex({ "uuid": 1 })
db.vms.createIndex({ "name": 1 })
db.vms.createIndex({ "ip": 1 })
db.vms.createIndex({ "hostname": 1 })
// Same for other collections
db.volumes.createIndex({ "uuid": 1 })
db.hosts.createIndex({ "hostname": 1 })
db.hypervisors.createIndex({ "hostname": 1 })
Search went from 15 seconds to under 100ms. Users are happy and less complaints.