Unix Technical Forum

efficient way to determine "is there a nonzero value"

This is a discussion on efficient way to determine "is there a nonzero value" within the MySQL forums, part of the Database Server Software category; --> On 28 Nov, 10:34, Daniel Marcinkowski <daniel.marcinkow...@gmail.com> wrote: > Hi all, > > in my database I have a ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 02-28-2008, 11:30 AM
Captain Paralytic
 
Posts: n/a
Default Re: efficient way to determine "is there a nonzero value"

On 28 Nov, 10:34, Daniel Marcinkowski <daniel.marcinkow...@gmail.com>
wrote:
> Hi all,
>
> in my database I have a very large table. I want to check one column of
> this table if there exists a nonzero value in it. The ideas I came up
> with is counting or using the max function. However, these functions
> have to check all values in a column. The most efficient way would be to
> use a function which just scans for the first nonzero value and then, if
> one was found, returns a true (or 1).
>
> Does anyone have an idea?
> --
> Dan


"The most efficient way would be to use a function which just scans
for the first nonzero value and then, if one was found, returns a true
(or 1)."
On what basis do you say that?

Why not create an index on the column and just use something like
SELECT
1
WHERE column > 0
LIMIT 1

(If values can be negative too add "OR column < 0" at the end of teh
WHERE clause)


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 02-28-2008, 11:30 AM
lawpoop@gmail.com
 
Posts: n/a
Default Re: efficient way to determine "is there a nonzero value"

On Nov 28, 4:34 am, Daniel Marcinkowski
<daniel.marcinkow...@gmail.com> wrote:
> Hi all,
>
> in my database I have a very large table. I want to check one column of
> this table if there exists a nonzero value in it. The ideas I came up
> with is counting or using the max function. However, these functions
> have to check all values in a column. The most efficient way would be to
> use a function which just scans for the first nonzero value and then, if
> one was found, returns a true (or 1).
>
> Does anyone have an idea?
> --
> Dan


How about

SELECT column
FROM table
WHERE column <> 0

?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 02-28-2008, 11:30 AM
Rik Wasmus
 
Posts: n/a
Default Re: efficient way to determine "is there a nonzero value"

On Wed, 28 Nov 2007 20:06:19 +0100, <lawpoop@gmail.com> wrote:

> On Nov 28, 4:34 am, Daniel Marcinkowski
> <daniel.marcinkow...@gmail.com> wrote:
>> Hi all,
>>
>> in my database I have a very large table. I want to check one column of
>> this table if there exists a nonzero value in it. The ideas I came up
>> with is counting or using the max function. However, these functions
>> have to check all values in a column. The most efficient way would beto
>> use a function which just scans for the first nonzero value and then,if
>> one was found, returns a true (or 1).

> How about
>
> SELECT column
> FROM table
> WHERE column <> 0


Which would select all rows, and the OP just wants to know 'wether there
are any rows with a non-zero value' in the most efficient way possible.

I agree with the Captain, add a key to the column, and then run a query
(like yours), limited to one:
SELECT column FROM table WHERE column != 0 LIMIT 1
--
Rik Wasmus
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 02-28-2008, 11:30 AM
lark
 
Posts: n/a
Default Re: efficient way to determine "is there a nonzero value"

== Quote from Rik Wasmus (luiheidsgoeroe@hotmail.com)'s article
> On Wed, 28 Nov 2007 20:06:19 +0100, <lawpoop@gmail.com> wrote:
> > On Nov 28, 4:34 am, Daniel Marcinkowski
> > <daniel.marcinkow...@gmail.com> wrote:
> >> Hi all,
> >>
> >> in my database I have a very large table. I want to check one column =

> of
> >> this table if there exists a nonzero value in it. The ideas I came up=
> >> with is counting or using the max function. However, these functions
> >> have to check all values in a column. The most efficient way would be=

> to
> >> use a function which just scans for the first nonzero value and then,=

> if
> >> one was found, returns a true (or 1).

> > How about
> >
> > SELECT column
> > FROM table
> > WHERE column <> 0

> Which would select all rows, and the OP just wants to know 'wether there=
> =
> are any rows with a non-zero value' in the most efficient way possible.
> I agree with the Captain, add a key to the column, and then run a query =
> =
> (like yours), limited to one:
> SELECT column FROM table WHERE column !=3D 0 LIMIT 1
> -- =
> Rik Wasmus



it's bit unclear what he wants; he says nonzero value in it. this could be
construed as a nonzero value in the column, something like "123". what if he wants
a hit on a value such as this: "102". the other thing he's saying is: "have to
check all values in a column" which again is a bit murky. maybe there is a bit of
language barrier in here.

ya'll jump the gun without knowing exactly what he wants.
--
POST BY: lark with PHP News Reader ;o)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 02-28-2008, 11:30 AM
Norman Peelman
 
Posts: n/a
Default Re: efficient way to determine "is there a nonzero value"

Captain Paralytic wrote:

>
> You mean "add a 'LIMIT 1'" just like the LIMIT 1 I already had in the
> first response to this thread huh!


Yes indeed sir!

Norm
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:03 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com