473,624 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting rows assigned to index value?

In my MYISAM table I have an index (Index_A) on 2 fields (Field_A,
Field_B). There are millions of rows in the table. The cardinality of
Index_A is 53. I think a query to count the number of rows that match
a pair of values for Field_A and Field_B should execute blindingly fast
(in the index count the number of leaf nodes for the B-Tree node
representing the indexed pair of values).

My query looks like:
SELECT count(*) FROM `table_A` WHERE `Field_A`=5 and `Field_B`=1

Why does it take 20 seconds for this query? And how can it be made
faster?

Thanks

Oct 5 '05 #1
2 1747
>In my MYISAM table I have an index (Index_A) on 2 fields (Field_A,
Field_B). There are millions of rows in the table. The cardinality of
Index_A is 53.
Which doesn't indicate that there aren't 99% of the rows with
Field_A=5. If it used the index to find the rows with matching
Field_A, how many would it get?
I think a query to count the number of rows that match
a pair of values for Field_A and Field_B should execute blindingly fast
(in the index count the number of leaf nodes for the B-Tree node
representing the indexed pair of values).

My query looks like:
SELECT count(*) FROM `table_A` WHERE `Field_A`=5 and `Field_B`=1

Why does it take 20 seconds for this query?
How many rows does it return? If it returns several million rows,
it is likely to take that much just scrolling the screen.

Since you don't have an index on (Field_A, Field_B), it will need
to fetch all the records with Field_A=5 to check Field_B.

does
EXPLAIN SELECT count(*) FROM `table_A` WHERE `Field_A`=5 and `Field_B`=1

indicate that it's using the index?
And how can it be made
faster?


Delete all the rows, but I don't think that's what you want.

Gordon L. Burditt
Oct 5 '05 #2

Gordon Burditt wrote:
In my MYISAM table I have an index (Index_A) on 2 fields (Field_A,
Field_B). There are millions of rows in the table. The cardinality of
Index_A is 53.
Which doesn't indicate that there aren't 99% of the rows with
Field_A=5. If it used the index to find the rows with matching
Field_A, how many would it get?


The data is pretty evenly distributed over Field_A and Field_B
I think a query to count the number of rows that match
a pair of values for Field_A and Field_B should execute blindingly fast
(in the index count the number of leaf nodes for the B-Tree node
representing the indexed pair of values).

My query looks like:
SELECT count(*) FROM `table_A` WHERE `Field_A`=5 and `Field_B`=1

Why does it take 20 seconds for this query?


How many rows does it return? If it returns several million rows,
it is likely to take that much just scrolling the screen.

SELECT count(*) ALWAYS returns ONE row
Since you don't have an index on (Field_A, Field_B), it will need
to fetch all the records with Field_A=5 to check Field_B.

does
EXPLAIN SELECT count(*) FROM `table_A` WHERE `Field_A`=5 and `Field_B`=1

indicate that it's using the index?
And how can it be made
faster?


Delete all the rows, but I don't think that's what you want.

Gordon L. Burditt

Actually I guess the index was not (optimized?).
After running OPTOMIZE on the table the query became very fast

Oct 6 '05 #3

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

Similar topics

16
1715
by: walexand | last post by:
I use the database mysql v.4. My problem is... I have a select like: select * from user where language = "de"; the result are then: id name =================== 1 max
6
6475
by: Brendan.Collins | last post by:
Hi I have a javascript problem that has been annoying me for two days now and thought that a javascript expert might have the magic solution. I am populating a table dynamically from the database and I am trying to allow the user to order the rows in the table using up and down arrows. The function I am using is:
8
3848
by: DrNoose | last post by:
Hi! I'm writing a program that is supposed to read a line of input, count the words and the number of occurrences of each letter. Then it should prints the number of occurrences of each letter that appears in the input line. For example, if I typed "I say Hi." it should give the following output: 3 words 1 a
7
4800
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
3
4868
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that the best method? Do you have a sample of how to do this?
4
7728
by: cbrichards via SQLMonster.com | last post by:
I have a stored procedure that will execute with less than 1,000 reads one time (with a specified set of parameters), then with a different set of parameters the procedure executes with close to 500,000 reads (according to Profiler). In comparing the execution plans, they are the same, except for the actual and estimated number of rows. When the proc runs with parameters that produce reads that are less than 1,000 the actual and...
0
1222
by: Chia Lee Lee | last post by:
Hello… I have problem when counting the number of records, which is based on the start date and end date. I have tried to use message box to prompt the result, but the result given is zero(0).Actually there have records in database which fulfill the “WHERE” clause condition in sql statement. Thanks for yours help… Carol, /* Below is my code sample(VB.NET) */ sDate = dtStart.Value.ToShortDateString eDate = dtEnd.Value.ToShortDateString
1
3047
by: Newmanbt | last post by:
I've got the following code testlabel.Text = allquestions.Select().Count.ToString(); GridView1.DataSource = allquestions.Select(); GridView1.DataBind(); testlabel.Text = GridView1.Rows.Count.ToString(); allquestions.Select().Count.ToString(); is reporting the correct value, but GridView1.Rows.Count.ToString(); is reporting the incorrect value (it is reporting 0)
2
2687
by: jed | last post by:
Hello what i have done here is read an xml file into a untyped dataset. the values i can see in a datagrid but how would i gain access to these rows, they are in a txt format in the dataset. Here are some of the xml values DayNumber="0" First="2964" Last="1"> <Value Index="0">- 3.8 mA</Value> <Value Index="1">- 3.8 mA</Value> <Value Index="2">- 3.8 mA</Value> <Value Index="3">- 3.8 mA</Value> <Value Index="4">- 3.8 mA</Value>
0
8233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8170
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8334
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6108
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.