简体   繁体   中英

What's best data structure for primary key and secondary key in database?

In my python project, I have an employee map. The key is employee number , and value is an object with employee information , and the location field in the value is unique , for example,

mapA = {12: {name: 'John Ma', location: 'US'}, 25: {name: 'Richard Yan', location: 'Eng'}, ...}

Also there is another map, the key is location , and value is another objects with something else. such as,

mapB = {'US': objectS1, 'Eng': objectS2, ...}

Now I want to build one map between employee number and objectS which name is mapC.

mapC = {12: objectS1, 25: objectS2 ...}

I must find the location in the object of mapA, then get the key which points to this object. Then use this key and objectS in mapB to build the new mapC. In this way, the time complexity is O(N^2).

To reduce time complexity, first I build mapD which key is location and value is employee number through traversing mapA, then I can get mapC easily.

Is there any better solution for that question?

BTW, another question come into my mind: what's best data structure for primary key and secondary key in database?

For example, here is one table,

| Username  | Password  | Email_Addr |
--------------------------------------
| Username1 | Password1 | Email 1    |
| Username2 | Password2 | Email 2    |
| Username3 | Password1 | Email 3    |

and username is first primary key and Password is secondary key, what's best data structure to store them to get best performance?

IMO, to simply, combine username and password as one compound key. and we sort those records according to the following compare function,

bool key_compare(string key1, string key2){
    return key1+key2 > key2+key1;
}

and those information are stored in B_tree .

Edit 2 From this document, we know, those two indexes can be implemented with hash table and inverted table . In the table above, The hash table save the information of Username and the address of each record,

{Username1: 'record1 address', Username2: 'record2 address'} and the inverted table hold the information of Password and record address table.

{Password1: ['record1 address', 'record3 address'], Password2: ['record2 address']}

Edit 1 I want to know how database to handle them, for example, MySQL... Is there any hashMap beside B_tree? thanks in advance.

I think a better solution would be to use one map that maps id to employee and to keep all the information about said employee in 'Employee object.

So based on your example Employee would have two values 1. name 2. location

Your issue seems to be that location is a key to another map and not the information you'd like to grab. Why not have a 'Location' object that holds all this information? You'd be able to eliminate the need for a second and third map.

Create a "location" object with information such as: 1. Country 2. Latitude\\longitude

So now

    mapA = {12: employee1, 43: employee2, ...}

employee1 has information such as: name = 'John' location = usLocation

usLocation is an object of type location and has all the relevant information encapsulated inside of it.

As for your second question I haven't dealt much with SQL so I'll let someone else tackle that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM