In the context of massive machine-type communications (mMTCs), the narrowband Internet-of-Things (NB-IoT) technology is envisioned to efficiently and reliably deal with massive device connectivity. Hence, it relies on a tailored random access (RA) procedure, for which theoretical and empirical analyses are needed for a better understanding and further improvements. This article presents the first data-driven analysis of NB-IoT RA, exploiting a large-scale measurement campaign. We show how the RA procedure and performance are affected by network deployment, radio coverage, and operators' configurations, thus complementing simulation-based investigations, mostly focused on massive connectivity aspects. A comparison with the performance requirements reveals the need for procedure enhancements. Hence, we propose a machine learning (ML) approach and show that RA outcomes are predictable with good accuracy by observing radio conditions. We embed the outcome prediction in an RA-enhanced scheme and show that optimized configurations enable power consumption reduction of at least 50%. We also make our data set available for further exploration, toward the discovery of new insights and research perspectives.