-- Database Indexes for Performance Improvement
-- Run this script ONCE to add indexes to shopdb
-- Estimated time: 30 seconds to run
-- Impact: 50-80% faster queries

-- Check if indexes already exist first
-- (Safe to re-run - will show errors for existing indexes but won't break anything)

USE shopdb;

-- Printers table indexes
CREATE INDEX idx_printers_printerid ON printers(printerid);
CREATE INDEX idx_printers_machineid ON printers(machineid);
CREATE INDEX idx_printers_modelid ON printers(modelid);
CREATE INDEX idx_printers_csfname ON printers(printercsfname);
CREATE INDEX idx_printers_ipaddress ON printers(ipaddress);
CREATE INDEX idx_printers_active ON printers(isactive);

-- Machines table indexes
CREATE INDEX idx_machines_machineid ON machines(machineid);
CREATE INDEX idx_machines_number ON machines(machinenumber);
CREATE INDEX idx_machines_alias ON machines(alias);
CREATE INDEX idx_machines_typeid ON machines(machinetypeid);
CREATE INDEX idx_machines_modelid ON machines(modelnumberid);
CREATE INDEX idx_machines_businessunit ON machines(businessunitid);
CREATE INDEX idx_machines_printerid ON machines(printerid);

-- Models table indexes
CREATE INDEX idx_models_modelid ON models(modelnumberid);
CREATE INDEX idx_models_vendorid ON models(vendorid);
CREATE INDEX idx_models_active ON models(isactive);

-- Vendors table indexes
CREATE INDEX idx_vendors_vendorid ON vendors(vendorid);
CREATE INDEX idx_vendors_isprinter ON vendors(isprinter);
CREATE INDEX idx_vendors_active ON vendors(isactive);

-- PC table indexes
CREATE INDEX idx_pc_pcid ON pc(pcid);
CREATE INDEX idx_pc_machinenumber ON pc(machinenumber);
CREATE INDEX idx_pc_hostname ON pc(hostname);
CREATE INDEX idx_pc_active ON pc(isactive);

-- PC Network Interfaces table indexes
CREATE INDEX idx_pc_network_pcid ON pc_network_interfaces(pcid);
CREATE INDEX idx_pc_network_ip ON pc_network_interfaces(IPAddress);

-- Business Units table indexes
CREATE INDEX idx_businessunits_id ON businessunits(businessunitid);
CREATE INDEX idx_businessunits_active ON businessunits(isactive);

-- Machine Types table indexes
CREATE INDEX idx_machinetypes_id ON machinetypes(machinetypeid);
CREATE INDEX idx_machinetypes_funcacct ON machinetypes(functionalaccountid);

-- Applications table (if search is slow)
CREATE INDEX idx_applications_appid ON applications(appid);
CREATE INDEX idx_applications_name ON applications(appname);
CREATE INDEX idx_applications_active ON applications(isactive);

-- Subnets table
CREATE INDEX idx_subnets_id ON subnets(subnetid);
CREATE INDEX idx_subnets_ipstart ON subnets(ipstart);
CREATE INDEX idx_subnets_ipend ON subnets(ipend);

-- Show completion message
SELECT 'Indexes created successfully!' AS Status;
SELECT 'Note: Some may show as errors if they already exist - this is normal.' AS Note;
