473,809 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Two queries VS one query--an index usage question

I have two tables, one is Student, which has studentid, personid,
grade, fees as its columns.
The other one is Person, which has personid, firstname, lastname,
address as its columns.
When given a studentid, I need to get the student' info, such as
lastname from the db tables.
I can do it with two queries as the following:
1. mysql> select personid FROM Student where studentid=1;
2. mysql> select lastname FROM Person where personid="the id I got
above";

Or I can do it with one query:
3. mysql> select p.lastname from Student s, Person p where
s.studentid=1 AND s.personid=p.pe rsonid

Both works. However, I can't tell which one is actually faster, since
MySQL shows me "1 row in set (0.00 sec)" for all these three queries.
Both studentid and personid are primary keys, so they are
automatically indexed.
Will both indexes be used in query number 3? I saw people said that
MySQL could use in a query only one index...Does it mean only one
index will be used in query number 3? I am wondering which one will
give better performance.
Thank you in advance.
Jul 20 '05 #1
1 1800
Henry wrote:
Both works. However, I can't tell which one is actually faster, since
MySQL shows me "1 row in set (0.00 sec)" for all these three queries.


Well, how much faster than 0.00 seconds can it get? ;-)
I'm kidding -- I realize the performance issue will become more
important later when there are thousands of records in those tables.

MySQL should be able to use multiple indexes in a query. I don't know
where the comment about one index per query came from, perhaps it was
true in some ancient version of MySQL.

Try using the EXPLAIN command to get more information about its use of
indexes.
http://dev.mysql.com/doc/mysql/en/EXPLAIN.html

Also consider there is some overhead to running two queries instead of
one. I recommend that you use the JOIN query.

Regards,
Bill K.
Jul 20 '05 #2

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

Similar topics

5
1889
by: Pedro Alves | last post by:
Hi. I'm having some trouble on the use of indexes. The querys below are exactly the same but refer to different months. One case uses indexes, the other doesn't. Is there anything I can do? Increasing index mem size? Query 2 hash 9105 entries matching the given conditions Query 2 hash 9248 entries matching the given conditions
1
1196
by: Staff, Alexander | last post by:
Hi, I created a simple table (name char200, zip char10, city char200, street char200, id int) and filled some data, appr. 250 000 records, in it. I tested accessing some rows (select id from address where id = 44444;, select * from address where id between 33333, 333444) with an unique index on id and without an index. EXPLAIN tells me in both cases that it does NOT use the index. I tested this with using the name column (select name from...
8
4840
by: Andr? Queiroz | last post by:
Hi, I have a table with 10M records and col A has a index created on it. The data on that table has the same value for col A on all 10M records. After that I insert diferent values for that column but my queries do not use the index I created for that column. Is there any way I can force the usage of the index or to ommit a value on the index creation, like 0 (zeroes) or spaces? Thanks in advance, André Queiroz
8
5272
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is developing a web-based application, one part of which involves allowing the user the ability to page through transaction "history" information. The _summary_ history table will have the following fields: ServiceName, Date, User-Ref1, User-Ref2,...
3
2780
by: paul | last post by:
Hi, is there any way in db2 v8.2.3 (fp10) on aix 5.3 to know if indexes on table are used. The only method i know is to create an event monitor on statement and then pass this worload to the design advisor. Is there a faster way. Again, i some some queries that worked better on db2 v8.1. I migrated this into db2 v8.2 and those queries take table fullscan now instead of hash join. I guess that the optimser changed between these two...
4
1266
by: Greg P | last post by:
If i connect to my database and select all tables and views. Then I drag a few differnt tables onto differnt tabs on the same form. I know that the data isn't bound until the tab header is clicked. Yet is the entire datasource brougth into ram or just the table (or tables that are in the view)? What I'm wondering is if I should create multiple data sources for each particular form to save memory usage or not? Thanks,
2
2550
by: peter.prib | last post by:
Hi, I am having trouble getting DB2 to select an index I have created for a spatial location. Table definition create table gps_paf_address ( gnaf_address gnaf_address not null ,GNF_PID varchar(015) not null
2
1394
by: natG | last post by:
On a 3 column table, the PK consists of all 3 columns. I have queries on the first two columns, as well as on the last two columns. (Select * where column3=x and column2=y.) I was hoping that this same pk index will suffice for the latter (reverse-scan is on), but its doing a table scan. Have I misinterpreted what a reverse-scan is? Please advise. Thanks. nat
4
10652
by: crazy_jutt | last post by:
hi all, i heard that db2 ignores indexes when using any column function on the column which has index on it. but i have seen db2 using indexes even when using column function. what is the criteria of db2 about picking index or not picking index when there is a column function ? i am using function on one column like below
0
9721
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
9601
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
10637
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...
0
10115
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
7660
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
5550
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
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.