I have a table (MYSQL 8) with apx. 100M records storing various bits of stock data (price, date, etc.), and query A below runs in < 1s, but query B takes over 2mins. Among other indices, I've got an index on the date
, and the primary key for the table is (symbol
, date
). What would cause such a significant difference between the two queries, and what might speed up the poor performer?
Query A:
SELECT symbol, MIN(date)
FROM Stocks
WHERE date BETWEEN '2015-01-01' AND '2020-01-01'
GROUP BY symbol
Query B
SELECT symbol, MIN(date)
FROM Stocks
WHERE date BETWEEN '2015-01-01' AND '2020-01-01' AND market_cap > 20
GROUP BY symbol
The other challenge I'm facing is that at times I want to filter by market_cap
, but other times by other numerical fields (gross_profit
, total_assets
, etc.). The query is being generated by a form with a number of optional inputs that are tied to params.
Table schema
CREATE TABLE IF NOT EXISTS cri_v0_995 (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
company_id MEDIUMINT UNSIGNED NOT NULL,
dt DATE NOT NULL,
price DECIMAL(18, 2),
market_cap DECIMAL(12, 4),
div_yield DECIMAL(4,2), -- this is always 0
price_to_nte DOUBLE,
price_to_mte DOUBLE,
nte_to_price DECIMAL(16, 10),
ante_to_price DECIMAL(16, 10),
ate_to_price DECIMAL(18, 10),
price_to_sales DOUBLE,
price_to_earnings DOUBLE,
cur_liq_to_mcap DECIMAL(4, 2), -- this is always 0
net_liq_to_mcap DOUBLE,
g3_rev_growth_and_trend DECIMAL(14, 10),
p_cri_score DECIMAL(14, 10),
f_cri_score DECIMAL(10, 7),
cri_score DECIMAL(14, 10),
PRIMARY KEY (id),
FOREIGN KEY (company_id) REFERENCES companies (id),
UNIQUE KEY (company_id, dt)
);
Note that there are a couple cols that I'm unsure about. They've always been zeros but I don't know what the intent may be behind them atm.
(edit 1 to address missing GROUP BY
s)
(edit 2 adding table schema)