# SOT SQL question

**URL:** <https://www.ruby-forum.com/t/sot-sql-question/146055>\
**Category:** Rails\
**Created:** [August 16, 2008, 9:53pm UTC](https://www.ruby-forum.com/t/sot-sql-question/146055 "2008-08-16T21:53:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [August 16, 2008, 9:53pm UTC](https://www.ruby-forum.com/t/sot-sql-question/146055/1 "2008-08-16T21:53:29Z")

</div>

I have an items tables, a tokens table, and a items\_tokens join table.  
There  
can be multiple joins for an items, tokens pair, i.e. a token appears  
multiple  
times in an item. I would like to increment a column in the tokens for  
EACH  
time it appears in in the join table. It seems that “UPDATE tokens JOIN  
items\_tokens ON items\_tokens.token\_id = tokens.id set occurrences =  
occurrences + 1 WHERE items\_tokens.item\_id = 339;” (for item 339) would  
do the  
trick but it only increments each occurrences at most once, i.e.  
duplicates  
are removed before incrementing occurrences. Is there a way to do this  
in ONE  
SQL statement?

TIA,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [August 17, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/sot-sql-question/146055/2 "2008-08-17T19:15:27Z")

</div>

Why don’t you just use a counter\_cache on Token?

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [August 18, 2008, 5:24am UTC](https://www.ruby-forum.com/t/sot-sql-question/146055/3 "2008-08-18T05:24:54Z")

</div>

ActiveRecord was great for prototyping the proof of concept. But it’s  
too  
slow for production. I need to keep it in all SQL.

Thanks,  
Jeffrey

Quoting Rein H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [August 18, 2008, 9:56pm UTC](https://www.ruby-forum.com/t/sot-sql-question/146055/4 "2008-08-18T21:56:02Z")

</div>

Sorry, I wasn’t specific enough. It is too slow for one of my  
applications  
for just one of the models w/ a join table. When a cronjob that is  
invoked  
every five minutes occasionally runs for over 20 minutes with actual  
data, an  
application is too slow and it is time to start profiling and  
benchmarking.  
The results are interesting, to me at least.

Pure Active Record: 7.2sec  
SQL w/ join table: 0.77sec  
SQL w/out join table: 0.20sec

The application is an adaptive RSS reader. Every article has many words  
w/  
statistics for each word (occurrences, click-thrus, up-votes,  
down-votes,  
etc.). It is faster to regenerate the word list on the fly each time  
and  
select on the word’s string field than use a join table with select with  
joins  
on the IDs.

As always, YMMV. So measure, then cut (code).  
Jeffrey

Quoting Rein H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [August 18, 2008, 6:24am UTC](https://www.ruby-forum.com/t/sot-sql-question/146055/5 "2008-08-18T06:24:31Z")

</div>

LMAO. Good luck with that. And don’t tell all the people running Rails  
with AR in production. I’m sure they’ll be devastated to hear that  
their sites are too slow.

On Aug 17, 11:23 pm, “Jeffrey L. Taylor” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
