Domanda

I've a table named messages where users of my local hub store their messages(kind of like a web-forums). Currently, a majority of users are participating and I get nearly 30 to 50 new entries to my table everyday.

Since this has been going on for past few years, we've got nearly 100,000 rows of data in table. The table structure is kind of like this. Where fid is the PRIMARY and ip and id(nickname) are just INDEX.

I was using this kind of query uptil now; and then iterating the resultset in luasql as shown in this link. This, according to me, consumes a lot of time and space(in buffers).

`msg` VARCHAR(280) NOT NULL,
`id` VARCHAR(30) NOT NULL,
`ctg` VARCHAR(10) NOT NULL,
`date` DATE NOT NULL COMMENT 'date_format( %m/%d/%y )',
`time` TIME NOT NULL COMMENT 'date_format( %H:%i:%s )',
`fid` BIGINT(20) NOT NULL AUTO_INCREMENT,
`ip` CHAR(39) NOT NULL DEFAULT '127.0.0.1'

My problem is that now-a-days, we've switched to new API of PtokaX and the number of requests to read and write have increased dramatically. Since, I recently read about MySQL procedures, I was thinking if these procedures are a faster or safer way of dealing with this situation.

SELECT *
FROM ( SELECT *
    FROM `messages`
    ORDER BY `fid` DESC
    LIMIT 50 ) AS `temp`
ORDER BY `fid` ASC;

P.S.

We get around one request to read one message every 7 to 10 seconds on average. On weekends, it rises to around one every 3 seconds.

Please let me know if anything more is required.


TO SUM UP

Is their a way that I can call a stored procedure and get the final result in a smaller time. Current query(and method) takes it nearly 3 seconds to fetch and organize the data.

È stato utile?

Soluzione

Few things regarding your query:

SELECT *
FROM ( SELECT *
    FROM `messages`
    ORDER BY `fid` DESC
    LIMIT 50 ) AS `temp`
ORDER BY `fid` ASC;
  1. Never SELECT * (all); always specify a column list (what you need)
  2. Subqueries typically cost more (for sorting & storage)
  3. If you are trying to fetch the bottom '50', trying using a BETWEEN clause instead

You can always see what you're query is doing by using EXPLAIN. I would try the following query:

SELECT `msg`, `id`, `ctg`, `date`, `time`, `fid`, `ip` FROM `messages` 
WHERE `fid` > (SELECT MAX(`fid`)-50 FROM `messages`)
ORDER BY `fid` 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top