473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

q: BIT OPERATIONS and their usefullness..

I noticed that DB2 does not support bitwise operations but I found
some UDFs that seem to give this functionality..

There seems to be a consensus that bitoperations are not usefull (or
bad practice) in sql.. but I am currious though,

say I have a table with a few columns..

attribute_color, attributegroup_2, attributegroup_3....

each one storing a bitstring defining the which of a bunch of values
this attribute contains..

for example...

a choice of (red, yellow, blue, green) can be stored as 0011 (in
attribute_color) if blue and green are the selected colors.. this
makes counting attribute combinations very simple and efficiant over a
lot of rows..

if I wanted to know which items are blue AND green I could simply do a
select count(*) from attribute_table where attribute_color BITAND 11
== 11...
likewise all green colors can be counted with attribute_color BITAND
01=01..

for a large number of attributes and options and adhoc counts of
attribute combinations this works very well.. confining all the data
to one row.

I realise that the software has to keep track of bit positions and
meanings.. but thats little to worry about considering the trade off
between this method and lots of joins or pivoting attribute tables,
is it not?

is this a poor method of storing attributes (that belong to a
multivalue group) ? seems to work nice and fast...

Can someone tell me why this is bad practice? and why is it that there
are no built in BIT operations to do this?

thanks,
Vasili
Dec 13 '07 #1
3 2662
vasilip wrote:
I noticed that DB2 does not support bitwise operations but I found
some UDFs that seem to give this functionality..
I beg to differ:
http://publib.boulder.ibm.com/infoce.../r0052628.html
There seems to be a consensus that bitoperations are not useful (or
bad practice) in sql.. but I am currious though,

say I have a table with a few columns..

attribute_color, attributegroup_2, attributegroup_3....

each one storing a bitstring defining the which of a bunch of values
this attribute contains..

for example...

a choice of (red, yellow, blue, green) can be stored as 0011 (in
attribute_color) if blue and green are the selected colors.. this
makes counting attribute combinations very simple and efficiant over a
lot of rows..

if I wanted to know which items are blue AND green I could simply do a
select count(*) from attribute_table where attribute_color BITAND 11
== 11...
likewise all green colors can be counted with attribute_color BITAND
01=01..

for a large number of attributes and options and adhoc counts of
attribute combinations this works very well.. confining all the data
to one row.

I realise that the software has to keep track of bit positions and
meanings.. but thats little to worry about considering the trade off
between this method and lots of joins or pivoting attribute tables,
is it not?

is this a poor method of storing attributes (that belong to a
multivalue group) ? seems to work nice and fast...

Can someone tell me why this is bad practice? and why is it that there
are no built in BIT operations to do this?
Your example is actually very well suited to explain it.
CREATE TABLE stuff(id INT, red INT, yellow INT, blue INT, green INT,
color INT GENERATED ALWAYS
AS (red * 8 + yellow * 4 + blue * 2 + green));
CREATE INDEX redidx ON stuff(red);
CREATE INDEX yellowidx ON stuff(yellow);
CREATE INDEX blueidx ON stuff(blue);
CREATE INDEX greenidx ON stuff(green);
CREATE INDEX color ON stuff(color);

SELECT id FROM stuff WHERE red = 1 AND green = 1

Allows the the DBMS to use technology that is called INDEX-ANDING.
That is the DBMS will find all the rows that are red , all those that
are green and intersect them efficiently.
The cost will increase below linear as the table size increases.

By contrast
SELECT id FROM stuff WHERE BITAND(color, 3) = 3

The DBMS will have to look at each row in the table and apply the BITAND
function (DB2 won't even look at that index).
Cost will lineary increase with the size of the table.

BIT functions aren't always bad. E.g. it is impractical to
index 100 columns each being either "on" or "off".
The cost to INSERT and DELETE operations would be to high and there is
too much overhead.

So as always. Never say never - or always :-)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Dec 13 '07 #2
FYI, BIT* functions were added for IBM Content Manager.
Ironically the guy who drove them in is a relational database legend.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Dec 14 '07 #3
>There seems to be a consensus that bit operations are not useful (or bad practice) in SQL.. <<

Bad practice. The SQL Standard was written to be hardware
independent, and not to depend on binary implementations like high-
end, low-end, 8, 16, 32 or 64 bit words, etc.

1NF requires that all values be scalars, so bit operations are a
violation of the foundations of RDBMS.

From a programming viewpoint, they are proprietary and do not port.
The constraints needed to keep them valid become nightmares of
complexity ("Hey, this column says we have a pregnant man with yellow
eyes!"). Then indexing and searching are messy and hard to maintain.
>say I have a table with a few columns..
attribute_color, attributegroup_2, attributegroup_3....
each one storing a bitstring defining the which of a bunch of values
this attribute contains.. <<

Why not normalize the schema instead? What happens when you add
purple? Or drop green? You have to do a serious ALTER TABE and UPDATE
>if I wanted to know which items are blue AND green I could simply do a select count(*) from attribute_table where attribute_color BITAND 11 == 11... <<
SQL does not use a == sign; you are going to low-level assembly
languages and C programming. We would write something like this:

SELECT sku
FROM Inventory
WHERE item_color IN ('green', 'blue')
GROUP BY sku
HAVING COUNT(DISTINCT item_color) = 2;

It would be better to use the Land or Pantone color numbers in a real
database, but you get the idea.

With an index this is fast on a row-oriented DB; it is almost
instantaneous with a hashed or column-oriented DB.
Dec 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: sarah chang | last post by:
I want to implement a variety of SOAP methods (with Apache SOAP) using just one method of one Java class, with the SOAP method name passed as a parameter to the Java method and the SOAP parameters...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
4
by: AMDRIT | last post by:
Gang, I always get confused when it comes to 1's and 0's. I would like to perform a bitwise operation on a value based on checked boxes. Am I doing this right? assuming...
3
by: bob | last post by:
In an effort to keep my UI responsive I have begun to run my time intensive methods on their own thread and in particular the database inserts. I open my DB connection in the constructor and close...
21
by: ashish.sadanandan | last post by:
Hi, I haven't done a lot of C++ programming (done a lot of it in C) and the reason why I'm considering switching now is also the question I'm posting here. I have some library functions that...
13
by: Immanuel Goldstein | last post by:
Obtained under the Freedom of Information Act by the National Security Archive at George Washington University and posted on the Web today, the 74-page "Information Operations Roadmap" admits that...
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
4
by: Jono | last post by:
Hi Everyone, As it says in the title, I'm looking for a way to display a page while long running operations are performed on the server. Ideally, I'd like some way to push the current request...
7
by: Flavio | last post by:
Hi, I have been playing with set operations lately and came across a kind of surprising result given that it is not mentioned in the standard Python tutorial: with python sets, intersections ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.