-- ============================================
-- DATABASE OPTIMIZATION INDEXES
-- For Simplified Product Import Performance
-- ============================================
-- Run this script on your production database to improve import speed
-- Expected improvement: 20-30% faster database operations

-- ============================================
-- 1. PRODUCTS TABLE INDEXES
-- ============================================

-- Index on SKU for fast product lookup (most critical)
-- This is checked for every product import to see if it exists
CREATE INDEX IF NOT EXISTS idx_products_sku ON products(sku);

-- Index on attribute_family_id for faster joins
CREATE INDEX IF NOT EXISTS idx_products_attribute_family ON products(attribute_family_id);

-- Index on type for faster filtering
CREATE INDEX IF NOT EXISTS idx_products_type ON products(type);

-- ============================================
-- 2. PRODUCT_FLAT TABLE INDEXES
-- ============================================

-- Index on product_id for faster flat creation
CREATE INDEX IF NOT EXISTS idx_product_flat_product_id ON product_flat(product_id);

-- Index on SKU for faster lookups
CREATE INDEX IF NOT EXISTS idx_product_flat_sku ON product_flat(sku);

-- Index on status and visibility for product listing
CREATE INDEX IF NOT EXISTS idx_product_flat_status_visible ON product_flat(status, visible_individually);

-- ============================================
-- 3. PIVOT TABLES (RELATIONSHIPS)
-- ============================================

-- Product Categories Pivot Table
CREATE INDEX IF NOT EXISTS idx_product_categories_product ON product_categories(product_id);
CREATE INDEX IF NOT EXISTS idx_product_categories_category ON product_categories(category_id);
CREATE INDEX IF NOT EXISTS idx_product_categories_both ON product_categories(product_id, category_id);

-- Product Branches Pivot Table (branches_products)
CREATE INDEX IF NOT EXISTS idx_branches_products_product ON branches_products(product_id);
CREATE INDEX IF NOT EXISTS idx_branches_products_branch ON branches_products(branch_id);
CREATE INDEX IF NOT EXISTS idx_branches_products_both ON branches_products(product_id, branch_id);

-- Note: product_channels table does not exist in this database schema
-- Channels are handled differently in this application

-- ============================================
-- 4. CATEGORIES TABLE INDEXES
-- ============================================

-- Index on parent_id for hierarchical queries
CREATE INDEX IF NOT EXISTS idx_categories_parent ON categories(parent_id);

-- Index on status for active category filtering
CREATE INDEX IF NOT EXISTS idx_categories_status ON categories(status);

-- ============================================
-- 5. CATEGORY_TRANSLATIONS TABLE INDEXES
-- ============================================

-- Critical for category name lookups during import
CREATE INDEX IF NOT EXISTS idx_category_translations_name ON category_translations(name(191));
CREATE INDEX IF NOT EXISTS idx_category_translations_locale ON category_translations(locale);
CREATE INDEX IF NOT EXISTS idx_category_translations_category_id ON category_translations(category_id);

-- Composite index for name + locale lookups (with length limit to avoid key too long error)
CREATE INDEX IF NOT EXISTS idx_category_translations_name_locale ON category_translations(name(191), locale);

-- Note: product_translations table does not exist in this database schema
-- Product translations are handled differently in this application

-- ============================================
-- 6. PRODUCT_INVENTORIES TABLE INDEXES
-- ============================================

-- Index for inventory lookups
CREATE INDEX IF NOT EXISTS idx_product_inventories_product ON product_inventories(product_id);
CREATE INDEX IF NOT EXISTS idx_product_inventories_source ON product_inventories(inventory_source_id);

-- ============================================
-- 8. PRODUCT_IMAGES TABLE INDEXES
-- ============================================

-- Index for product image queries
CREATE INDEX IF NOT EXISTS idx_product_images_product ON product_images(product_id);

-- ============================================
-- VERIFICATION QUERIES
-- ============================================
-- Run these to verify indexes were created successfully

-- Check all indexes on products table
-- SHOW INDEXES FROM products;

-- Check all indexes on product_flat table
-- SHOW INDEXES FROM product_flat;

-- Check all indexes on pivot tables
-- SHOW INDEXES FROM product_categories;
-- SHOW INDEXES FROM product_branches;

-- Check all indexes on category_translations
-- SHOW INDEXES FROM category_translations;

-- ============================================
-- PERFORMANCE TESTING
-- ============================================
-- Test query performance before and after indexes

-- Test 1: Product lookup by SKU (should be very fast)
-- EXPLAIN SELECT * FROM products WHERE sku = 'TEST-SKU-001';

-- Test 2: Category lookup by name
-- EXPLAIN SELECT * FROM category_translations WHERE name = 'Test Category' AND locale = 'ar';

-- Test 3: Product flat lookup
-- EXPLAIN SELECT * FROM product_flat WHERE product_id = 1;

-- ============================================
-- MAINTENANCE
-- ============================================
-- Run these periodically to maintain performance

-- Analyze tables to update statistics
-- ANALYZE TABLE products;
-- ANALYZE TABLE product_flat;
-- ANALYZE TABLE product_categories;
-- ANALYZE TABLE product_branches;
-- ANALYZE TABLE category_translations;

-- Optimize tables to defragment
-- OPTIMIZE TABLE products;
-- OPTIMIZE TABLE product_flat;
-- OPTIMIZE TABLE product_categories;
-- OPTIMIZE TABLE product_branches;

-- ============================================
-- NOTES
-- ============================================
-- 1. These indexes are safe to add on production
-- 2. They will slightly slow down INSERT/UPDATE operations but dramatically speed up SELECT queries
-- 3. For import operations, the SELECT speedup far outweighs the INSERT slowdown
-- 4. Indexes are automatically maintained by MySQL
-- 5. Run ANALYZE TABLE periodically to keep statistics up to date
-- 6. Expected improvement: 20-30% faster import times after adding these indexes
