# Tailing database

**URL:** <https://forums.percona.com/t/tailing-database/1037>\
**Category:** Other MySQL® Questions\
**Created:** [December 16, 2008, 3:52pm UTC](https://forums.percona.com/t/tailing-database/1037 "2008-12-16T15:52:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonasl](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@jonasl](https://forums.percona.com/u/jonasl)\
**Post date:** [December 16, 2008, 3:52pm UTC](https://forums.percona.com/t/tailing-database/1037/1 "2008-12-16T15:52:06Z")

</div>

I’m having a table with 1M rows and i’m always fetching the rows that was just inserted (auto\_increment) and the queries get slower the more rows i’ll insert. Can i query the last 10 “content” without using ‘ORDER BY’ ?

mysql\> describe logfile\G\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 1. row \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Field: id Type: int(11) Null: NO Key: PRIDefault: NULL Extra: auto\_increment\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 2. row \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Field: content Type: varchar(5000) Null: YES Key:Default: NULL Extra:

And my query:

mysql\> SELECT content FROM logfile ORDER BY id DESC LIMIT 20;

---

<div class="post-metadata">

**Author:** ![januzi](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@januzi](https://forums.percona.com/u/januzi)\
**Post date:** [December 16, 2008, 5:11pm UTC](https://forums.percona.com/t/tailing-database/1037/2 "2008-12-16T17:11:03Z")

</div>

1. modify query:

| [B]Quote:[/B] |
| 

SELECT id, content FROM logfile ORDER BY id DESC LIMIT 20;

 |

2. run query as usual 3. fetch data and remember the biggest id 4. put that id into the file 5. modify query again:

| [B]Quote:[/B] |
| 

SELECT id, content FROM logfile where id \> $old\_id ORDER BY id DESC LIMIT 20;

 |

6. You have got old id inside a file, so all You have to do is to get it from file

In three steps Your script (or application) should:

- read old id from the file
- use it in query
- write last id to the file

You could check queries:

| [B]Quote:[/B] |
| 

explain SELECT content FROM logfile ORDER BY id DESC LIMIT 20;  
explain SELECT id, content FROM logfile where id \> $old\_id ORDER BY id DESC LIMIT 20;

 |

(put right value as $old\_id) Second one should examine less rows, there should by range as type.

---

<div class="post-metadata">

**Author:** ![jonasl](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@jonasl](https://forums.percona.com/u/jonasl)\
**Post date:** [December 17, 2008, 1:41am UTC](https://forums.percona.com/t/tailing-database/1037/3 "2008-12-17T01:41:14Z")

</div>

Oh thank you! I’m using memcache and i think that what you describe can be done easy.
