# Why am I getting an extra SELECT before save!?

**URL:** <https://www.ruby-forum.com/t/why-am-i-getting-an-extra-select-before-save/186962>\
**Category:** Rails\
**Created:** [April 15, 2010, 12:30am UTC](https://www.ruby-forum.com/t/why-am-i-getting-an-extra-select-before-save/186962 "2010-04-15T00:30:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [April 15, 2010, 12:30am UTC](https://www.ruby-forum.com/t/why-am-i-getting-an-extra-select-before-save/186962/1 "2010-04-15T00:30:21Z")

</div>

In my app, I want to create a new record if it doesn’t exist, and update  
some fields if it does.

Looking at the debug log, and it appears that I’m doing something that’s  
causing an extra SELECT. The basic synopsis:

sta = Station.find\_by\_station\_id(station\_id)  
if sta.nil?  
Station.create(params)  
else  
sta.p1 = “white port”  
sta.p2 = “lemon juice”  
sta.save!  
end

# The Station.create() isn’t a problem. But if it takes the ‘else’ clause, the SQL trace looks suspicious. (Hand edited for clarity…):

Station Load: SELECT \* FROM `stations` WHERE (`stations`.`station_id` =  
‘WPLJ’) LIMIT 1

SQL BEGIN

Station Load: SELECT `stations`.id FROM `stations` WHERE  
(`stations`.`station_id` = BINARY ‘WPLJ’ AND `stations`.id \<\> 12095)  
LIMIT 1

Station Update: UPDATE `stations` SET `p1` = “white port”, `p2` = “lemon  
juice” WHERE `id` = 12095

# COMMIT

It seems like just the UPDATE would suffice, so what is that second  
SELECT doing? And what can I do to make it go away? 🙂

TIA.

- ff

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [April 15, 2010, 12:39am UTC](https://www.ruby-forum.com/t/why-am-i-getting-an-extra-select-before-save/186962/2 "2010-04-15T00:39:01Z")

</div>

On Apr 14, 2010, at 3:30 PM, Fearless F. wrote:

> Station.create(params)  
> (`stations`.`station_id` =  
> ‘WPLJ’) LIMIT 1
> 
> SQL BEGIN
> 
> Station Load: SELECT `stations`.id FROM `stations` WHERE  
> (`stations`.`station_id` = BINARY ‘WPLJ’ AND `stations`.id \<\> 12095)  
> LIMIT 1

Any chance you have a validates\_uniqueness\_of :station\_id in your  
model? If so, the above select is checking for that.

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [April 15, 2010, 12:54am UTC](https://www.ruby-forum.com/t/why-am-i-getting-an-extra-select-before-save/186962/3 "2010-04-15T00:54:24Z")

</div>

Philip H. wrote:

> Any chance you have a validates\_uniqueness\_of :station\_id in your  
> model? If so, the above select is checking for that.

Give that man a cigar! Yep, that’s exactly what’s going on – thanks!

I believe it’s safe to remove the uniqueness constraint since my code is  
checking for existence of a station before creating a new one anyway.  
If anyone wants to warn me of any gotcha’s about doing this, now would  
be a good time to tell me.

- ff

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [April 15, 2010, 1:01am UTC](https://www.ruby-forum.com/t/why-am-i-getting-an-extra-select-before-save/186962/4 "2010-04-15T01:01:51Z")

</div>

On Apr 14, 2010, at 3:54 PM, Fearless F. wrote:

> be a good time to tell me.   
> Are you going to check for that condition _every_ time you create that  
> model? In scripts, via the console, somewhere else in the code?

If you remove that constraint you’ll need to remember this every  
time… and at some point you’ll forget. I always do 😕

I’d leave it alone if it were me.

-p

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [April 15, 2010, 1:34am UTC](https://www.ruby-forum.com/t/why-am-i-getting-an-extra-select-before-save/186962/5 "2010-04-15T01:34:03Z")

</div>

On Apr 14, 2010, at 4:06 PM, Fearless F. wrote:

> > -p
> 
> Point well taken. Right now, that’s the only code that modifies the  
> Station table, and I’m doing a massive (initial) load and update –  
> removing the constraint really does speed things up. But heeding your  
> advice, I’ll reinstate the constraint after this initial load  
> finishes.  
> (What? Of COURSE I’ll remember! 🙂

🙂 You could also do my\_model\_instance.save(false) which would bypass  
the validations entirely. As long as you’re sure your initial data  
load is legit.

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [April 15, 2010, 1:06am UTC](https://www.ruby-forum.com/t/why-am-i-getting-an-extra-select-before-save/186962/6 "2010-04-15T01:06:55Z")

</div>

Philip H. wrote:

> Are you going to check for that condition _every_ time you create that  
> model? In scripts, via the console, somewhere else in the code?
> 
> If you remove that constraint you’ll need to remember this every  
> time… and at some point you’ll forget. I always do 😕
> 
> I’d leave it alone if it were me.
> 
> -p

Point well taken. Right now, that’s the only code that modifies the  
Station table, and I’m doing a massive (initial) load and update –  
removing the constraint really does speed things up. But heeding your  
advice, I’ll reinstate the constraint after this initial load finishes.  
(What? Of COURSE I’ll remember! 🙂

Thanks.

-ff
