473,788 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

quieries taking a while - key problem?

crabpot8
40 New Member
I am decent at queries, but recently i made a new DB with about 50000+ entries. Queries are taking almost 20+ seconds, and i think it is that i dont have decently structured keys - this is the first time I was the one that created the DB structure. Here is the info that seemed relevant - ANY HELP APPRECIATED

There is one 'primary' key - a mix of server, timestamp, and ipaddress
This is a few server access logs put into a database

Also - if you know of a good tutorial on keys - i have been trying to read up - but everywhere either deals with what they are or how to optimize queries, and none of them seem to deal with the issue together

crabpot8

_______________ _______________ ______________

Field Type Null Default
ip_address char(15) No
timestamp timestamp No CURRENT_TIMESTA MP
filename varchar(1000) Yes NULL
http_status smallint(6) Yes NULL
bytes_sent int(11) Yes NULL
client_info text Yes NULL
client_id text Yes NULL
client_stat_res ults text Yes NULL
file_size int(11) Yes NULL
send_time int(11) Yes NULL
resends smallint(6) Yes NULL
failed_resends smallint(6) Yes NULL
pres_id smallint(6) Yes NULL
file_time int(11) Yes NULL
helix_ enum('a', 'b') No a


Indexes:
Keyname Type Cardinality Field
PRIMARY PRIMARY 55485 ip_address
timestamp
helix_


Space usage:
Type Usage
Data 39,552 KiB
Index 0 B
Total 39,552 KiB
Row Statistics:
Statements Value
Format Compact
Rows 73,709
Row length ø 549
Row size ø 549 B
Creation Feb 04, 2008 at 12:36 PM
_______________ _______________ _______________ ___

here are the types of query i have to run ran - each took over 20 seconds
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(*) FROM rmaccess WHERE timestamp BETWEEN 20080225004627 AND 20080225014627
  2.  
  3. SELECT COUNT(*) FROM rmaccess WHERE timestamp BETWEEN 20080225014627 AND 20080225024627
  4.  
  5. SELECT COUNT(*) FROM rmaccess WHERE timestamp BETWEEN 20080225024627 AND 20080225034627
Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that.

MODERATOR
Mar 11 '08 #1
3 1378
mwasif
802 Recognized Expert Contributor
Hi crabpot8,

Your query is not using the INDEX at all. Use EXPLAIN to confirm this.

Reason: In multiple column indexes, they are always read from left to right. You have defined the index in the order
Expand|Select|Wrap|Line Numbers
  1. ip_address
  2. timestamp
  3. helix_
To properly use the index for your said query, you must have to create a combine index in the following order
Expand|Select|Wrap|Line Numbers
  1. timestamp
  2. ip_address
  3. helix_
Let us know if you need more help.
Mar 12 '08 #2
crabpot8
40 New Member
Hi crabpot8,

Your query is not using the INDEX at all. Use EXPLAIN to confirm this.

Reason: In multiple column indexes, they are always read from left to right. You have defined the index in the order
Expand|Select|Wrap|Line Numbers
  1. ip_address
  2. timestamp
  3. helix_
To properly use the index for your said query, you must have to create a combine index in the following order
Expand|Select|Wrap|Line Numbers
  1. timestamp
  2. ip_address
  3. helix_
Let us know if you need more help.

Thanks a lot! I actually did it a little differently - I am not sure if its maximum effectiveness but it definitely works better. I needed the three columns to be unique together, but I am generating charts of server stats so i normally use the timestamps in my queries. So i did this - any opinions or am i headed in the right direction?
Expand|Select|Wrap|Line Numbers
  1. Primary Key on timestamp, helix_
  2. Unique Key on ip_address, timestamp, helix_
These are in the correct order that i have them in my table. My queries work in a reasonable time now, but if I made the second key timestamp, ip_address, helix_ , would it help even more?


I still would love a good tutorial on how to make indexes on data like this that does not need a unique ID, but has a column or set of columns that will suffice.

Thanks for the help!
~crabpot
Mar 12 '08 #3
crabpot8
40 New Member
Hm - I tried changing my keys up and here are the results -

SQL Statement -
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT( timestamp )
  2. FROM rmaccess
  3. WHERE timestamp
  4. BETWEEN 20080225034627
  5. AND 20080225044627
SQL Statement (with different timestamp values) Ran about 150 times on both



Keys version 1
  • Primary - timestamp,helix _
  • Unique - ip_address, timestamp, helix_

Explain Of SQL

Expand|Select|Wrap|Line Numbers
  1. id - 1
  2. select_type - SIMPLE
  3. table - rmaccess
  4. type - range
  5. possible_keys - PRIMARY
  6. key - PRIMARY
  7. key_len - 4
  8. ref - NULL
  9. rows - 6
  10. Extra - Using where; Using index
  11.  
Speed Test Results
Expand|Select|Wrap|Line Numbers
  1. 1st - 0.97661185264587
  2. 2nd - 0.71671605110168 
  3. 3rd - 0.67619180679321 

Keys version 2
  • Primary - timestamp,helix _
  • Unique - timestamp, ip_address, helix_

Explain of SQL
Expand|Select|Wrap|Line Numbers
  1. id - 1
  2. select_type - SIMPLE
  3. table - rmaccess
  4. type - range
  5. possible_keys - PRIMARY, unique
  6. key - PRIMARY
  7. key_len - 4
  8. ref - NULL
  9. rows - 6
  10. Extra - Using where; Using index
  11.  
Speed Test Results
Expand|Select|Wrap|Line Numbers
  1. 1st - 1.0137181282043 
  2. 2nd - 0.66393899917603 
  3. 3rd - 0.67385196685791 
Interesting Note: phpmyadmin said this with version two:
'PRIMARY and INDEX keys should not both be set for column `timestamp`'



So from looking at the explains - I am guessing (i did a little reading, but have not had time to do a lot) That the two queries are equivalent with the different indexes. Strangely enough - perhaps this is what phpmyadmin meant by should not be using for two queries - the version one, with timestamp in the middle of the unique rather than the front - ran faster in subsequent tests (as in perhaps it cached more). The only reason i can think of is that if it used two keys in the query it might try to cache them both, versus if it just used one it might cache more and return other results faster.

~crabpot8

PS - i know this is a lot to dig results from, but I really cant find a good tutorial on this
Mar 12 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
5068
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar = getter(file); What type should I give to `getter' so that the compiler does not issue
0
1718
by: RSH | last post by:
I have a simple Windows Form, where i have a datagridview. The user can switch between databases and select the tables via DropDownLists. I am having a problem that I can't really pinpoint when it happens but occassionally tables take a long time to load... They are not the same tables, and it doesn't appear consistent. But a simple table that contains a single row can take up to 30 seconds to load, while only seconds before a table...
13
4244
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them from the html page (replace the words in the html page with blank space) I'm new to python and could use a little push in the right direction, any ideas on how to implement this? Thanks!
2
4306
by: Adi | last post by:
Hello friend, Lately, i cam accross a small problem that has causing quite more pain. Scanf hangs whenever i try to re- take input into the same char array. I've pasted below excerpt of my code: ---------------------------------------------------------------------------------------------------------------------------------------- short cond =0; unsigned char intStr;
8
1633
by: so many sites so little time | last post by:
this time i am going to use the scripts from the book and just change the names to match what i am trying to do now i just got an error that i know shouldnt be an error i think. this is the error: Could add the site entry because: Column count doesn't match value count at row 1. The query was INSERT INTO home (home_id, header, body, date_entered) VALUES ('Welcome!', 'What is a Progressive Parent? A Progressive Parent ... now i created...
6
4331
by: billyusa | last post by:
Hello all, im taking a intro to c++ class as part of my design comm, and having issues withthe codelab part (online). First off it doesnt give any help to what you are doing wrong. the prob is: Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a while loop to compute the sum of the cubes of the first n counting numbers, and store...
3
3429
by: tshad | last post by:
Using asp.net 2.0, I am finding that at times, the old javascript will still be there. I was working with it for a couple of hours and the changes seem to happen. But at the end of the day, I found that my last changes weren't taking affect. I was trying to delete my "alerts" before doing a final build but even after taking out all my alerts, when I built it and ran it - the alerts were still
3
1317
by: Robert Bevington | last post by:
Hi all, I ran into memory problems while tying to search and replace a very large text file. To solve this I break the file up into chunks and run the search and replace on each chunk. This works fine and has solved the OutOfMemory problem. However, on the last loop when the array c is written to CleanTMX, a number of 0x00 characters are written at the end of the file. This causes problems in a further XMLTransformation as this...
1
1940
by: sheldonlg | last post by:
I have inherited code with a TDC control. In this file, there are two javascripts of interest. One of these is a function, filter(), which is inside <script language=javascript></script>. The other is code inside <script language=javascript FOR=inquiry event=ondatasetcomplete()></script> where inquiry is the TDC.
0
9656
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...
1
10113
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
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
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
5402
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
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
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
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.