Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:29:25+00:00 2026-06-15T09:29:25+00:00

I have a data.table which has two keys: Year (10 levels) and MemberID (200,000

  • 0

I have a data.table which has two keys: Year (10 levels) and MemberID (200,000 levels). When I setkey, does setkey(MemberID, Year) result in different performance compare with setkey(Year, MemberID)? If so, which way will be better?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:29:26+00:00Added an answer on June 15, 2026 at 9:29 am

    The performance and speed of the key setting will depend on the key variable types. numeric columns will be slower than integer. character columns (when short strings) appear to be fast.

    eg

     library(data.table)
    
    set.seed(1)
     DIC <- data.table(year = sample(seq_len(10), 5e6, TRUE), id = sample(as.character(seq_len(2e5)), 5e6, TRUE), z = rnorm(5e6))
    DIC2 <- copy(DIC)
    DIF <-  data.table(year = sample(seq_len(10), 5e6, TRUE), id = sample(as.factor(seq_len(2e5)), 5e6, TRUE), z = rnorm(5e6))
    DIF2 <- copy(DIF)
    DNC <- data.table(year = sample(as.numeric(seq_len(10)), 5e6, TRUE), id = sample(as.character(seq_len(2e5)), 5e6, TRUE), z = rnorm(5e6))
    DNC2 <- copy(DNC)
    DCC <- data.table(year = sample(as.character(seq_len(10)), 5e6, TRUE), id = sample(as.character(seq_len(2e5)), 5e6, TRUE), z = rnorm(5e6))
    DCC2 <- copy(DCC)
     DII <- data.table(year = sample(seq_len(10), 5e6, TRUE), id = sample(seq_len(2e5), 5e6, TRUE), z = rnorm(5e6))
    DII2 <- copy(DII)
    

    Some timings

    # key of integer, character columns
    system.time(setkey(DIC, year ,id))
       user  system elapsed 
       3.21    0.11    3.31 
    system.time(setkey(DIC2, id, year))
       user  system elapsed 
       3.43    0.03    3.45 
    # key of integer factor columns
    system.time(setkey(DIF, year ,id))
       user  system elapsed 
       6.31    0.05    6.37 
    system.time(setkey(DIF2, id, year))
       user  system elapsed 
       6.44    0.06    6.54 
    # key of numeric, character columns
    system.time(setkey(DNC, year ,id))
       user  system elapsed 
       9.91    0.07   10.29 
    system.time(setkey(DNC2, id, year))
       user  system elapsed 
      10.11    0.07   10.34 
    # key of two character columns
    system.time(setkey(DCC, year ,id))
       user  system elapsed 
       3.34    0.05    3.40 
    system.time(setkey(DCC2, id, year))
       user  system elapsed 
       3.40    0.02    3.42 
    # key of two integer columns
    system.time(setkey(DII, year ,id))
       user  system elapsed 
       6.25    0.02    6.53 
    system.time(setkey(DII2, id,year))
       user  system elapsed 
       6.44    0.05    6.64 
    

    As to which way will be better. This will probably depend on what you are most likely to subset by alone more often.

    For example, you may need to get all the data for year 1.

    If you have set the key as year, id then you can use

    D[J(1)]
    

    but if the key was set as id, year then you would need

    D[J(unique(id),1), nomatch = 0]
    

    which is more typing and will take longer as it has to calculate unique(id) .

    There is a feature request FR#1007 that looks at allowing a secondary key, but this is not implemented yet. Currently there is a single key that can occupy more than one column.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which has columns of data that contain status. Two example
I have a projects table which has two foreign keys for users (user_id and
I have a data table which already has some values, plus it is getting
I have an access table which has some cells as blank ( no data
I have a table of data which has posts, then I have a separate
I have a table which contains data about which node has been visited. It
I have an iPhone app which has a Table View-based data input screen with
I have a huge data.table in R which contains the result of a experiment:
I have a table companies , which has two columns named name and address
I have two databases($db1, $db2) with exact table structure(table1). $db2.table1 has new rows which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.