Tell me more ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a table named test :

create table demo (name varchar(10), mark1 int, mark2 int);

I need the total of mark1 and mark2 for each row many times.

select name, (mark1 + mark2) as total from demo;

Which I am told is not efficient. I am not allowed to add a new total column in the table.

Can I store such business logic in Index?

I created a view

CREATE VIEW view_total AS SELECT name, (mark1 + mark2) as 'total' from demo;

I populated the demo table with:

DELIMITER $$
CREATE PROCEDURE InsertRand(IN NumRows INT)
    BEGIN
        DECLARE i INT;
        SET i = 1;
        START TRANSACTION;
        WHILE i <= NumRows DO
            INSERT INTO demo VALUES (i,i+1,i+2);
            SET i = i + 1;
        END WHILE;
        COMMIT;
    END$$
DELIMITER ;

CALL InsertRand(100000);

The execution time of

select sum(mark1 + mark2) from view_total;

and

select sum(total) from demo;

is same, 10 ms. So I have not gained any benefit of view. I tried to create index over the view with :

create index demo_total_view on view_total (name, total);

which failed with error :

ERROR 1347 (HY000): 'test.view_total' is not BASE TABLE

Any pointer about how do I prevent the redundant action of totaling the columns?

share|improve this question
cross posted at StackOverflow stackoverflow.com/questions/14937024/… – kevinsky Feb 18 at 13:38
@kevinsky True, I was not aware where this post belonged. Should I remove this post? – user1263746 Feb 18 at 13:41
I would give the same answer here as you received on StackOverflow so closing it is an option. – kevinsky Feb 18 at 13:44
@kevinsky How do I close this question? – user1263746 Feb 18 at 13:46
I've flagged it for followup. – kevinsky Feb 18 at 13:50
show 1 more comment

closed as not constructive by JNK Feb 18 at 13:57

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.

Browse other questions tagged or ask your own question.