-- MySQL Performance Configuration for Transaction Query Timeout Fix
-- Run these commands as MySQL root user or user with appropriate privileges

-- Increase max_statement_time (available in MySQL 5.7.8+)
-- Set to 300 seconds (5 minutes) instead of default 28800
SET GLOBAL max_statement_time = 300000;  -- in milliseconds
SET SESSION max_statement_time = 300000; -- for current session

-- Increase other relevant timeout settings
SET GLOBAL max_execution_time = 300000;  -- MySQL 8.0+ equivalent
SET SESSION max_execution_time = 300000;

-- Increase general query timeouts
SET GLOBAL wait_timeout = 600;           -- 10 minutes
SET GLOBAL interactive_timeout = 600;    -- 10 minutes

-- Increase lock timeouts
SET GLOBAL lock_wait_timeout = 300;      -- 5 minutes
SET GLOBAL innodb_lock_wait_timeout = 300; -- 5 minutes

-- Optimize InnoDB settings for better performance
SET GLOBAL innodb_buffer_pool_size = 1073741824;  -- 1GB (adjust based on available RAM)
SET GLOBAL innodb_flush_log_at_trx_commit = 2;    -- Less strict but faster
SET GLOBAL innodb_log_file_size = 268435456;      -- 256MB

-- Query cache settings (if available)
SET GLOBAL query_cache_size = 134217728;  -- 128MB
SET GLOBAL query_cache_type = ON;

-- Show current timeout settings for verification
SELECT 
    @@global.max_statement_time as global_max_statement_time,
    @@session.max_statement_time as session_max_statement_time,
    @@global.wait_timeout as global_wait_timeout,
    @@global.interactive_timeout as global_interactive_timeout,
    @@global.lock_wait_timeout as global_lock_wait_timeout,
    @@global.innodb_lock_wait_timeout as global_innodb_lock_wait_timeout;

-- For persistent changes, add these to your MySQL configuration file (my.cnf or my.ini):
-- [mysqld]
-- max_statement_time = 300000
-- max_execution_time = 300000
-- wait_timeout = 600
-- interactive_timeout = 600
-- lock_wait_timeout = 300
-- innodb_lock_wait_timeout = 300
-- innodb_buffer_pool_size = 1G
-- innodb_flush_log_at_trx_commit = 2
-- innodb_log_file_size = 256M
-- query_cache_size = 128M
-- query_cache_type = ON
