i have table that contain 23 colulmns and 31000 records
table structure is as follows
CREATE TABLE fee ( id int(11) NOT NULL auto_increment, studentid varchar(10) NOT NULL, month int(15) NOT NULL, year int(4) NOT NULL, feedate date NOT NULL, amount int(6) NOT NULL, total int(5) NOT NULL, amtrec int(5) NOT NULL, annual int(5) NOT NULL, others int(11) NOT NULL default ‘0’, paymenttype varchar(15) NOT NULL, books int(5) NOT NULL, uniform int(5) NOT NULL, checkno int(6) NOT NULL, checkclear varchar(10) NOT NULL, paytype varchar(15) NOT NULL, fine int(5) NOT NULL, feedue int(5) NOT NULL, annualrec int(11) NOT NULL, adrecd int(11) NOT NULL, discount int(11) NOT NULL, srno int(11) NOT NULL, DRAWN varchar(30) NOT NULL,
PRIMARY KEY (id)
)
I have to write a query to get the sum of total column where studentid is xyz
so i m writing SELECT sum(total) WHERE studentid=‘xyz’ GROUP BY studentid
Sir but i cant as studentid is a code that is logically made and now at this moment i can not change, second thing is i have to sum the ‘total’ column
not the studentid
Thanks For the help actually our database server got stuck up and have some problem so lets see tommorow what happen but i m really thank ful to u for helping me well if the problem still persist i will ping u back tommorow
i have a table which is as follows
CREATE TABLE fee ( id int(11) NOT NULL auto_increment, studentid varchar(10) NOT NULL, month int(15) NOT NULL, year int(4) NOT NULL, feedate date NOT NULL, amount int(6) NOT NULL, total int(5) NOT NULL, amtrec int(5) NOT NULL, annual int(5) NOT NULL, others int(11) NOT NULL default ‘0’, paymenttype varchar(15) NOT NULL, books int(5) NOT NULL, uniform int(5) NOT NULL, checkno int(6) NOT NULL, checkclear varchar(10) NOT NULL, paytype varchar(15) NOT NULL, fine int(5) NOT NULL, feedue int(5) NOT NULL, annualrec int(11) NOT NULL, adrecd int(11) NOT NULL, discount int(11) NOT NULL, srno int(11) NOT NULL, DRAWN varchar(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=89 ;
i am writing a query as
SELECT SUM(total) FROM fee WHERE studentid=‘xyz’ GROUP BY studentid
It takes 2 min or more to response
Why do yoy have to indexes studentid and studentid_2? On what fields are these indexes defined?
Also, why is key_len 12 for a field that is varchar(10) ?
I would drop the index, create it anew with a shorter length (max 10, but shorter may also work, dependent on the distribution of student_ids) and see if that helps.
Have you tried an EXPLAIN SELECT sum(total) WHERE studentid=‘xyz’ GROUP BY studentid ?
Does that give you any insights? i.e., is MySQL actually using the index you created?