473,657 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hashtable faster than SQL ?!!

Hi guys,

can a hashtable be faster than a sql server??
i made my own O/R mapping with an "entity cache" (caching all mapped
objects). So, when i get some data from my sql server, i map that data to an
entity-object and i leave a copy of it in the entity cache.

the entity cache is a simple hashtable, which stores as key:
the ID of the entity ( the same id as the primary key of the sql-table ) and
the entity type.. and as value: the object itself.

so, when i access the cache, i say: Contact contact = EntityCache.Get ( 5109,
typeof( Contact ) )

i just made some tests and i filled the cache with 1 mio. contacts.
the result: searching the hashtable for one value, costs mostly ~0.000.007
sec! (nanoseconds).. thats amazing!! my sql server can never reach such
values! my dev. machine is just an amd 2 ghz with 1 gb ram.

of course i can search in the cache only by id, but it is the same way as
the sql server would search for an index, isn'it? i thought about, to setup
an extra caching-server... just read a big sql-table and store everything in
the entity-cache..(updatin g/inserting/deleting or searching by other values
is another topic).
i cant believe it!

steven.
Nov 17 '05 #1
6 6540
Hi Steven,

There's nothing surprising. The Hashtable is (well, let's assume so) in the
RAM, and its searching algorithm is optimized for performance. The SQL
Server keeps indexes on disk, so even if the algorithm is similar, loading
the indexes from disk takes time (as well as transmitting the result set
over the network or over a named pipe).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Steven Wolf" <ap****@gmx.net > wrote in message
news:51******** *************** ***********@mic rosoft.com...
Hi guys,

can a hashtable be faster than a sql server??
i made my own O/R mapping with an "entity cache" (caching all mapped
objects). So, when i get some data from my sql server, i map that data to
an
entity-object and i leave a copy of it in the entity cache.

the entity cache is a simple hashtable, which stores as key:
the ID of the entity ( the same id as the primary key of the sql-table )
and
the entity type.. and as value: the object itself.

so, when i access the cache, i say: Contact contact = EntityCache.Get (
5109,
typeof( Contact ) )

i just made some tests and i filled the cache with 1 mio. contacts.
the result: searching the hashtable for one value, costs mostly ~0.000.007
sec! (nanoseconds).. thats amazing!! my sql server can never reach such
values! my dev. machine is just an amd 2 ghz with 1 gb ram.

of course i can search in the cache only by id, but it is the same way as
the sql server would search for an index, isn'it? i thought about, to
setup
an extra caching-server... just read a big sql-table and store everything
in
the entity-cache..(updatin g/inserting/deleting or searching by other
values
is another topic).
i cant believe it!

steven.


Nov 17 '05 #2
Hi Dmytro,

without taking care about the network-time, namepiping etc., ms-sql/indexes
never get such results as the hashtable do.. i saw better results on
PostgreSQL...

are you sure the indexes are stored on the harddisk? i thought they were in
the RAM..

i am curious if sql server 2005 will conquer the speed as the hashtable do...

steven


"Dmytro Lapshyn [MVP]" wrote:
Hi Steven,

There's nothing surprising. The Hashtable is (well, let's assume so) in the
RAM, and its searching algorithm is optimized for performance. The SQL
Server keeps indexes on disk, so even if the algorithm is similar, loading
the indexes from disk takes time (as well as transmitting the result set
over the network or over a named pipe).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Steven Wolf" <ap****@gmx.net > wrote in message
news:51******** *************** ***********@mic rosoft.com...
Hi guys,

can a hashtable be faster than a sql server??
i made my own O/R mapping with an "entity cache" (caching all mapped
objects). So, when i get some data from my sql server, i map that data to
an
entity-object and i leave a copy of it in the entity cache.

the entity cache is a simple hashtable, which stores as key:
the ID of the entity ( the same id as the primary key of the sql-table )
and
the entity type.. and as value: the object itself.

so, when i access the cache, i say: Contact contact = EntityCache.Get (
5109,
typeof( Contact ) )

i just made some tests and i filled the cache with 1 mio. contacts.
the result: searching the hashtable for one value, costs mostly ~0.000.007
sec! (nanoseconds).. thats amazing!! my sql server can never reach such
values! my dev. machine is just an amd 2 ghz with 1 gb ram.

of course i can search in the cache only by id, but it is the same way as
the sql server would search for an index, isn'it? i thought about, to
setup
an extra caching-server... just read a big sql-table and store everything
in
the entity-cache..(updatin g/inserting/deleting or searching by other
values
is another topic).
i cant believe it!

steven.


Nov 17 '05 #3
Steven Wolf wrote:
[...snip...]
without taking care about the network-time, namepiping etc., ms-sql/indexes never get such results as the hashtable do.. i saw better results on
PostgreSQL... [...snip...] i am curious if sql server 2005 will conquer the speed as the hashtable

do...
[...snip...]

Don't expect any database to be even remotely as fast as a "simple" memory
access...
We are using Oracle 9 /10 as a database and even loading 1,000,000 rows of
data from file into memory and searching for a particular object (including
object creation) is faster than a simple query (first execution) against the
index of 1,000,000 rows in the database.
Nov 17 '05 #4


Steven Wolf wrote:
can a hashtable be faster than a sql server??


Try thinking about "when X can X be faster than a SQL server?....", well
certainly if the SQL server uses one for it's implementation :)

Index'es in SQL are usually done as some kind of sorted tree's, which
are pretty fast for lookup. Hashtables (with proper hash-functions) are
*faster*.

The reason for choosing a sorted tree is that it has other nice
properties. Most importantly, it allows sorted traversal of the
elements, a thing which every "order by" and "join", pretty much depend on.

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #5
It's possible that indexes reside in memory (provided they are small and
already loaded from DB), but even then, they are strored in the SQL server's
process space and searching an index from a client will be much slower than
searching a in process hashtable as it always involves a client/server
transition.
If however, you only consider SQL internal implementation of the index
tables and it's power compared to .NET's Hashtable lookup, I would expect
SQL to be on par.

Willy.
"Steven Wolf" <ap****@gmx.net > wrote in message
news:DF******** *************** ***********@mic rosoft.com...
Hi Dmytro,

without taking care about the network-time, namepiping etc.,
ms-sql/indexes
never get such results as the hashtable do.. i saw better results on
PostgreSQL...

are you sure the indexes are stored on the harddisk? i thought they were
in
the RAM..

i am curious if sql server 2005 will conquer the speed as the hashtable
do...

steven


"Dmytro Lapshyn [MVP]" wrote:
Hi Steven,

There's nothing surprising. The Hashtable is (well, let's assume so) in
the
RAM, and its searching algorithm is optimized for performance. The SQL
Server keeps indexes on disk, so even if the algorithm is similar,
loading
the indexes from disk takes time (as well as transmitting the result set
over the network or over a named pipe).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Steven Wolf" <ap****@gmx.net > wrote in message
news:51******** *************** ***********@mic rosoft.com...
> Hi guys,
>
> can a hashtable be faster than a sql server??
> i made my own O/R mapping with an "entity cache" (caching all mapped
> objects). So, when i get some data from my sql server, i map that data
> to
> an
> entity-object and i leave a copy of it in the entity cache.
>
> the entity cache is a simple hashtable, which stores as key:
> the ID of the entity ( the same id as the primary key of the
> sql-table )
> and
> the entity type.. and as value: the object itself.
>
> so, when i access the cache, i say: Contact contact = EntityCache.Get (
> 5109,
> typeof( Contact ) )
>
> i just made some tests and i filled the cache with 1 mio. contacts.
> the result: searching the hashtable for one value, costs mostly
> ~0.000.007
> sec! (nanoseconds).. thats amazing!! my sql server can never reach such
> values! my dev. machine is just an amd 2 ghz with 1 gb ram.
>
> of course i can search in the cache only by id, but it is the same way
> as
> the sql server would search for an index, isn'it? i thought about, to
> setup
> an extra caching-server... just read a big sql-table and store
> everything
> in
> the entity-cache..(updatin g/inserting/deleting or searching by other
> values
> is another topic).
>
>
> i cant believe it!
>
> steven.


Nov 17 '05 #6
thank you all for the answers.

"Steven Wolf" wrote:
Hi guys,

can a hashtable be faster than a sql server??
i made my own O/R mapping with an "entity cache" (caching all mapped
objects). So, when i get some data from my sql server, i map that data to an
entity-object and i leave a copy of it in the entity cache.

the entity cache is a simple hashtable, which stores as key:
the ID of the entity ( the same id as the primary key of the sql-table ) and
the entity type.. and as value: the object itself.

so, when i access the cache, i say: Contact contact = EntityCache.Get ( 5109,
typeof( Contact ) )

i just made some tests and i filled the cache with 1 mio. contacts.
the result: searching the hashtable for one value, costs mostly ~0.000.007
sec! (nanoseconds).. thats amazing!! my sql server can never reach such
values! my dev. machine is just an amd 2 ghz with 1 gb ram.

of course i can search in the cache only by id, but it is the same way as
the sql server would search for an index, isn'it? i thought about, to setup
an extra caching-server... just read a big sql-table and store everything in
the entity-cache..(updatin g/inserting/deleting or searching by other values
is another topic).
i cant believe it!

steven.

Nov 17 '05 #7

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

Similar topics

2
20304
by: Mark | last post by:
I'm using an enumerator to iterate through a HashTable that contains all numeric keys. I'd like to iterarate through the HashTable based on the ordered keys. Is there a quick way to do this? Thanks! -Mark IDictionaryEnumerator myEnumerator = myHashTable.GetEnumerator(); while ( myEnumerator.MoveNext() ) { //Please print ordered by Key ... Response.Write("<BR>" + myEnumerator.Key.ToString() + ", " + myEnumerator.Value.ToString())
4
17477
by: Vladimir C. | last post by:
Hashtable map = new Hashtable(); map = 10; map = 20; foreach(DictionaryEntry e in map) { e.Value = 100; Console.WriteLine("{0}: {1}", key, map); }
7
14726
by: davidw | last post by:
I always use NameValueCollection. But I read an article says the only differece between Hashtable and NameValueCollection is that NameValueCollection could accept more than one value with same key? I am not quite understand this. And I think NameValueCollection's Set is a nice method. If I use HashTable, how can I update a item in it? Do I have to check if it exists, and remove and add it again? Thanks.
4
10381
by: Matt C. | last post by:
I bet I know the answer already. I have a hashtable (hMaster) that holds several hashtables ("hTables") each of which holds other hashtables ("hColumns"). Presently, I am getting at the info I want thusly (this compiles, at least): Hashtable hTable = (Hashtable)hMaster; Hashtable hColumn = (Hashtable)hTable; return hColumn.ContainsKey(codeValue);
16
696
by: Sreekanth | last post by:
Hello, Is there any better collection than HashTable in terms of performance, when the type of the key is integer? Regards, Sreekanth.
5
7839
by: Dave | last post by:
Hi all, Is it possible to populate a listbox from a hashtable (complexbind) where the ValueMember of the listbox maps to the "key" member of the hashtable AND the DisplayMember of the listbox maps to the "value" member of the hashtable? I found that it is impossible to use a hashtable as a listbox datasource directly (from trial and error) and tried DirectCast-ing to an arraylist without any success. Before I spend anymore time in vain...
5
777
by: Vikas Kumar | last post by:
Hashtable 7/7/2006 2:37 AM I have this Hasttable windowList Hashtable windowList = new Hashtable(); To this Hashtable I add a form with a string as key in the following manner. The string is decided at runtime. FormX frm = new FormX(); frm.Show();
8
2072
by: Martin Pöpping | last post by:
Hello, I´ve implemented a Hashtable with Int-Keys and Double Values. Now I want to get the i-th Int-Key of my hashtable. How do I do that? I tried it like that: ICollection intKeys = myHashtable.Keys;
15
3090
by: Macca | last post by:
Hi, My app needs to potentially store a large number of custom objects and be able to iterate through them quickly. I was wondering which data structure would be the most efficient to do this,a hashtable or a generic list. Is using enumerators to iterate through the data structure a good idea? I'd appreciate any suggesstions or advice,
0
8420
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
8324
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
8842
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
8516
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
7353
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...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
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
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1733
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.